Asked by:
RetreiveEntityById jscript for CRM 2013 not working?

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
-
Hi,
The 2007 Web Service endpoints are no longer available in CRM 2013.
You need to update the code to use OrganizationService i.e. Modern App Soap Endpoint.
Regards,
Shraddha Dhingra
- Proposed as answer by Shraddha Dhingra Wednesday, December 11, 2013 3:06 AM
Tuesday, December 10, 2013 6:54 PM -
Thanks Shradha. I can see a blog post. it seems you refer to plugin as in http://blogs.msdn.com/b/shraddha_dhingra/archive/2013/11/26/connecting-to-crm-2013-online-soap-and-odata-endpoint-through-external-client-application-windows-forms-application.aspx
is there any pointers you can provide on how I can rewrite the jscript that I originally had
- Edited by CRM elite Tuesday, December 10, 2013 8:33 PM
Tuesday, December 10, 2013 8:30 PM -
Hello,
You can use OData endpoint to retrieve data from CRM:
http://msdn.microsoft.com/en-us/library/gg309549.aspx
http://msdn.microsoft.com/en-us/library/gg334427.aspx
Dynamics CRM MVP/ Technical Evangelist at SlickData LLC
My blog- Proposed as answer by Andrii ButenkoMVP, Moderator Tuesday, December 10, 2013 9:15 PM
Tuesday, December 10, 2013 9:15 PMModerator -
Hi,
Yes you can use the soap logger tool that will do that easily for you.
Just write your code in C#, which is lot easier then writing in JavaScript and run the soap logger tool it will generate the appropriate jScript.
It is for CRM 2011, but will work for CRM 2013 also.
Hope it helps.
Regards,
Shraddha Dhingra
- Proposed as answer by Shraddha Dhingra Wednesday, December 11, 2013 3:06 AM
Wednesday, December 11, 2013 3:03 AM -
Hi,
Even I am facing the same problem of upgrading my CRM 2011 organization to CRM 2013. The method RetrieveEntityById is throwing an error since 2013 will not support 2007 end points.
Can you kindly provide me the steps of what changes you made to this function to resolve this problem?
Thanks,
CRM Dev One
Tuesday, April 1, 2014 7:29 PM