locked
RetreiveEntityById jscript for CRM 2013 not working? RRS feed

  • Question

  • Hi, I have used this jscript in CRM 2011 online to RetreiveEntityById but its not working for CRM 2013! any help to find the replacement script will be highly appreciated.

    // Do not make any changes to this function
    function RetrieveEntityById(prmEntityName, prmEntityId, prmEntityColumns) {
    
        var resultXml, errorCount, msg, xmlHttpRequest, arrayEntityColumns, xmlEntityColumns;
    
        arrayEntityColumns = prmEntityColumns.split(",");
    
        for (var i = 0; i < arrayEntityColumns.length; i++) {
            xmlEntityColumns += "<q1:Attribute>" + arrayEntityColumns[i] + "</q1:Attribute>";
        }
    
        var authenticationHeader = Xrm.Page.context.getAuthenticationHeader();
    
        //Prepare the SOAP message.
        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>" +
        "<Retrieve xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +
        "<entityName>" + prmEntityName + "</entityName>" +
        "<id>" + prmEntityId + "</id>" +
        "<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>" +
        "<q1:Attributes>" +
        xmlEntityColumns +
       "</q1:Attributes>" +
        "</columnSet>" +
        "</Retrieve></soap:Body></soap:Envelope>";
    
        //call function to create Soap Request to ms crm webservice
        xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
        xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
        xmlHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Retrieve");
        xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
        xmlHttpRequest.send(xml);
    
        resultXml = xmlHttpRequest.responseXML;
    
        var errorCount = resultXml.selectNodes('//error').length;
    
        if (errorCount != 0) {
            var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
            alert("Error Message : " + msg);
        }
        else {
            return resultXml;
        }
    }

    I have used this function to populate the contract ID text box when a contract record is selected in another dropdown.  So on an on-change event, I have the following script that calls the RetreiveEntityById.

    // Only make changes to this function; you may add this function to Form Onload Event,
    // Field OnChange events etc.
    function GetContractId() 
    {
    
        var EntityName, EntityId, ContractEntityId, ContractEntityName, SerialNumber, ContractId, AccountEmailAddress, LookupFieldObject, LookupContractObject;
        var PrimaryContractLookupId, PrimaryContractLookupName, PrimaryContractLookupType;
        var resultXml;
    
        
        LookupFieldObject = Xrm.Page.data.entity.attributes.get('new_supportid');
        
    
        // If lookup field has value then the code will only run
        if (LookupFieldObject.getValue() != null) 
        {
    
            //Fetch and place Entity Id (GUID) and Name (String) form lookup field into local variables
            EntityId = LookupFieldObject.getValue()[0].id;
            EntityName = LookupFieldObject.getValue()[0].entityType;
    
                     resultXml = RetrieveEntityById(EntityName, EntityId, 'contractnumber,contractid');
            // In retrieved XML document check if it has accountnumber attribute
            if (resultXml != null && resultXml.selectSingleNode('//q1:contractnumber') != null) 
            {
    
                // If XML document has account number attribute then assign to local variable AccountNumber
                ContractId = resultXml.selectSingleNode('//q1:contractnumber').nodeTypedValue;
    
                //Display Account Number Value in a Message Box
                alert("Contract ID :" + ContractId);
    
                //If required then use the below code line to set value in field on form
                Xrm.Page.data.entity.attributes.get('new_contractid').setValue(ContractId);
            }
    
    
            
        }
    }

    ANY HELP with the above is Highly Appreciated. Thank you

    Tuesday, December 10, 2013 6:30 PM

All replies