locked
How to adapt an old JS used for CRM 4.0 to CRM 2015 RRS feed

  • Question

  • 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 4.0.
    It looks like an old syntax and I don't know how to adapt it to JS for CRM 2015 or CRM 2016. The script manage the user roles and roles name and I have retrive the data through fetchXML.
    I've been told to work with oDATA for crm 2015 or WebApi for CRM 2016.I haven't worked before with webservices and the requirement s are to use fetchXML so i guess i have to work with WebApi and i don't know how to adapt it.
    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="+
         "\"schemas.xmlsoap.org/.../envelope\" "+
         "xmlns:xsi=\"www.w3.org/.../XMLSchema-instance\" "+
         "xmlns:xsd=\"www.w3.org/.../XMLSchema\">" + 
         authenticationHeader+
         "<soap:Body>" + 
         // Specify the RetrieveMultiple message.
         "<RetrieveMultiple xmlns="+
         "\"schemas.microsoft.com/.../WebServices\">" + 
         // Specify that this is a QueryByAttribute query.
         "<query xmlns:q1="+
         "\"schemas.microsoft.com/.../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","schemas.microsoft.com/.../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="+
             "\"schemas.xmlsoap.org/.../envelope\" "+
             "xmlns:xsi=\"www.w3.org/.../XMLSchema-instance\" "+
             "xmlns:xsd=\"www.w3.org/.../XMLSchema\">" + 
             authenticationHeader+
             "<soap:Body>" + 
             // Specify the RetrieveMultiple message.
             "<RetrieveMultiple xmlns="+
             "\"schemas.microsoft.com/.../WebServices\">" + 
             // Specify that this is a QueryByAttribute query.
             "<query xmlns:q1="+
             "\"schemas.microsoft.com/.../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","schemas.microsoft.com/.../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);
            
        }		    
    }



    • Edited by zhaila Monday, September 12, 2016 5:19 PM
    Monday, September 12, 2016 4:39 PM

All replies

  • Below code will give a rough idea and starting point for your code customization for replacing legacy code with Web API

     var req = new XMLHttpRequest();
     req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/erm_companyroles?$select=erm_Name&$filter=erm_CompanyRoleId%20eq%20%27" + guidSource.text + "%27", false);
     req.setRequestHeader("OData-MaxVersion", "4.0");
     req.setRequestHeader("OData-Version", "4.0");
     req.setRequestHeader("Accept", "application/json");
     req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
     req.setRequestHeader("Prefer", "odata.include-annotations=\"OData.Community.Display.V1.FormattedValue\"");
     req.onreadystatechange = function() {

         if (this.readyState === 4) {
             req.onreadystatechange = null;
             if (this.status === 200) {

                 var results = JSON.parse(this.response);
                 if (results.value.length > 0) {
                 //do your operations here
                 }

             }

         }

     }


    If my response answered your question, please "mark the response as an answer" and also "vote as helpful". Regards, Hari. www.crm2011byhari.blogspot.com

    Tuesday, September 13, 2016 5:28 AM
  • @Hari Thank you! It might be a solution but I want the structure of how to retrieve the data in this function with fetch and set them to a lookup field.
    Tuesday, September 13, 2016 11:05 PM
  • you can get the values from the results as

    results.value[0].erm_Name

    you should do the same in //do your operations here placeholder i mentioned.

     and you can pass that value to any other function and use the same


    If my response answered your question, please "mark the response as an answer" and also "vote as helpful". Regards, Hari. www.crm2011byhari.blogspot.com

    Wednesday, September 14, 2016 2:53 AM
  • I would install the CRM REST Builder in your environment: 

    https://crmrestbuilder.codeplex.com/

    You select options to build a WebAPI query and it will generate all the JS code needed to execute the query and work with the results.  An invaluable tool!


    Friday, September 16, 2016 5:13 PM