0

I'm new at Microsoft Dynamics CRM and I'm working with CRM 2015. I'm working with CRM 2015 and I've been given to fix an error script developed for CRM 2011. It looks like an old syntax and I don't know how to adapt it to JS for CRM 2015. The script manage the user roles and roles name and I have retrieve the data through fetchXML.

function onChangeValutator()
{  
     var idUser = Xrm.Page.getAttribute("erm_valutatorid").getValue()[0].id;

     // Use the Xrm.Page.context.getAuthenticationHeader() method 
     // available from the CRM form to generate the Soap header text.
     var authenticationHeader = Xrm.Page.context.getAuthenticationHeader();

     // Define the SOAP XML to access Microsoft Dynamics CRM Web service.
     var xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + 
     "<soap:Envelope xmlns:soap="+
     "\"http://schemas.xmlsoap.org/soap/envelope/\" "+
     "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "+
     "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" + 
     authenticationHeader+
     "<soap:Body>" + 
     // Specify the RetrieveMultiple message.
     "<RetrieveMultiple xmlns="+
     "\"http://schemas.microsoft.com/crm/2007/WebServices\">" + 
     // Specify that this is a QueryByAttribute query.
     "<query xmlns:q1="+
     "\"http://schemas.microsoft.com/crm/2006/Query\" "+
     "xsi:type=\"q1:QueryByAttribute\">" + 
     // Query the customeraddress entity.
     "<q1:EntityName>erm_source</q1:EntityName>" + 
     // Set the columns you want to return.
     "<q1:ColumnSet xsi:type=\"q1:ColumnSet\">" + 
     "<q1:Attributes>" + 
     "<q1:Attribute>erm_roleid</q1:Attribute>" + 
     "</q1:Attributes>" + 
     "</q1:ColumnSet>" + 
     // Specify the attribute that you are querying on.
     "<q1:Attributes>" + 
     "<q1:Attribute>erm_sourceid</q1:Attribute>" + 
     "</q1:Attributes>" + 
     // Set the value of the attribute using the customerid 
     // value of the case record.
     "<q1:Values>" + 
     "<q1:Value xsi:type=\"xsd:string\">"+
     idUser+
     "</q1:Value>" + 
     "</q1:Values>" + 
     "</query>" + 
     "</RetrieveMultiple>" + 
     "</soap:Body>" + 
     "</soap:Envelope>";

    var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
    xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
    xmlHttpRequest.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");
    xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
    xmlHttpRequest.send(xml);
    var doc = xmlHttpRequest.responseXML;
    var source = doc.selectSingleNode("//BusinessEntity");

    var guidSource = source.childNodes[0];

    if (guidSource.text != null)
    {
        // Define the SOAP XML to access Microsoft Dynamics CRM Web service.
         xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + 
         "<soap:Envelope xmlns:soap="+
         "\"http://schemas.xmlsoap.org/soap/envelope/\" "+
         "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "+
         "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" + 
         authenticationHeader+
         "<soap:Body>" + 
         // Specify the RetrieveMultiple message.
         "<RetrieveMultiple xmlns="+
         "\"http://schemas.microsoft.com/crm/2007/WebServices\">" + 
         // Specify that this is a QueryByAttribute query.
         "<query xmlns:q1="+
         "\"http://schemas.microsoft.com/crm/2006/Query\" "+
         "xsi:type=\"q1:QueryByAttribute\">" + 
         // Query the customeraddress entity.
         "<q1:EntityName>erm_companyrole</q1:EntityName>" + 
         // Set the columns you want to return.
         "<q1:ColumnSet xsi:type=\"q1:ColumnSet\">" + 
         "<q1:Attributes>" + 
         "<q1:Attribute>erm_name</q1:Attribute>" + 
         "</q1:Attributes>" + 
         "</q1:ColumnSet>" + 
         // Specify the attribute that you are querying on.
         "<q1:Attributes>" + 
         "<q1:Attribute>erm_companyroleid</q1:Attribute>" + 
         "</q1:Attributes>" + 
         // Set the value of the attribute using the customerid 
         // value of the case record.
         "<q1:Values>" + 
         "<q1:Value xsi:type=\"xsd:string\">"+
         guidSource.text+
         "</q1:Value>" + 
         "</q1:Values>" + 
         "</query>" + 
         "</RetrieveMultiple>" + 
         "</soap:Body>" + 
         "</soap:Envelope>";
         
        xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
        xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
        xmlHttpRequest.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");
        xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
        xmlHttpRequest.send(xml);
        doc = xmlHttpRequest.responseXML;
        var role = doc.selectSingleNode("//BusinessEntity");
        var roleName = role.childNodes[0];
        
        var lookupItem = new Array();
        lookupItem[0] = new Object();
        
        lookupItem[0].name = roleName.text;
        lookupItem[0].entityType = "erm_companyrole";
        lookupItem[0].id = guidRSource.text;
        
        Xrm.Page.getAttribute("erm_valutatorroleid").setValue(lookupItem);
        
    }		    
}

2
  • Just an opinion, but if you want to be prepared for CRM 2016 and beyond you should abandon SOAP endpoint requests and switch to the ODATA endpoint.
    – Filburt
    Commented Sep 11, 2016 at 0:25
  • @Filburt I am willin to switch to ODATA but I don't haven't worked before with ODATA. I wanted a standart format to manage user roles in CRM 2016 using fetchXML. That's all I could think.
    – zhaila
    Commented Sep 11, 2016 at 10:52

1 Answer 1

1

Your queries are using the Dynamics CRM 4.0 web service endpoint, which is deprecated. Remove the authentication header from the SOAP message and replace the endpoint address "/mscrmservices/2007/CrmService.asmx" by "xrmservices/2011/organization.svc/web". A nice example can be found here.

With the introduction of the Web API in Dynamics CRM 2016 Microsoft dismissed the SOAP endpoint alltogether, but promised to continue supporting it for the coming years.

2
  • I got that.But I want to update form CRM 4.0 to CRM 2015. xrmservices/2011/organization.svc/web is for CRM 2011 and I wnat to convert it to ODATA but i don't know hoe tot adapt it.
    – zhaila
    Commented Sep 12, 2016 at 0:18
  • xrmservices/2011/organization.svc` was introduced with CRM 2011 and has been the only SOAP endpoint for CRM up to now. Commented Sep 13, 2016 at 5:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.