The CRM 2013 SDK states that jQuery in form scripts and ribbon commands is not recommended. So how do I RETRIEVE (or create, update, delete) attribute values for a related record using JavaScript? It turns out that using Ajax and the OData endpoint to manipulate the XMLHttpRequest object is the key.
In addition, I shall make use of several new XRM methods, including “Save”, “alertDialog” and “getClientUrl” method. So, here we go!
In a previous post I discussed . In this post I shall use several Xrm methods from the CRM 2013 SDK (version 6.0.2 at time of writing) below:
For example, the two fields outlined by the above are the “Parent Customer” and “Address” (composite) fields:
For the purpose of this post however, I shall use Ajax to work with the XmlHttpRequest object in JavaScript:
The “OnChange” event of the contact’s “Parent Customer” field triggers a JavaScript method, which does the following:
You can in fact do Step (4) in at least two different ways using Ajax: using an OData endpoint or using a SOAP endpoint (see the wonderful new detailed section “Use web service data in web resources – OData and Modern app SOAP endpoint” in the CRM 2013 SDK (version 6.0.2 at time of writing) if you don’t know what I’m talking about.
As I’m using OData in this post (hence the title, folks!), the first thing I test is the OrganizationData web service in CRM 2013 to retrieve a specific account record via its GUID. Again, the CRM 2013 SDK is your friend if you don’t know how to query using the OData endpoints.
Happily in the world of CRM 2013, the same OData URI as in CRM 2011, works like a charm and returns the correct Account information when I tested the following OData URL in IE10 (to see the unformatted view of the result, you might need to turn off feed reading view in IE 10 via Internet Options > Content > Feeds and Web Slices Settings > un-tick “Turn on feed reading view”).
name>/<CRM Organization Name>/XrmServices/2011/OrganizationData.svc/AccountSet?(guid’XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX’)
Now that we have the OData URL, in the good old days of CRM 2011 you can either:
To see the difference in syntax, go to the CRM 2013 SDK and look at the code examples given in “Use the OData endpoint with Ajax and Jscript web resources”.
However!!
The CRM 2013 SDK (version 6.0.2 at time of writing) has a big bold statement that says
“We do not recommend using jQuery in form scripts and ribbon commands. Most of the benefit provided by jQuery is that it allows for easy cross-browser manipulation of the DOM. This is explicitly unsupported within form scripts and ribbon commands.” One place that the CRM 2013 SDK recommends the use of jQuery is when you create your own HTML web resources to “provide user interfaces because it is an excellent cross-browser library [..] and there is no restriction against manipulating the DOM.”
Unfortunately different versions of jQuery library loaded interferes with each other in CRM 2013 and while its usage is no longer recommended in CRM 2013, you can mitigate this by checking for “conflict” (see this excellent for more detail).
Thus if you want to avoid jQuery library altogether but you still want to retrieve (or create, update or delete) attributes of an entity record, you will need to work with the native “XMLHttpRequest” object provided by the browser.
Fortunately the JavaScript for XMLHttpRequest object manipulation has not changed. One key difference to watch out for is that when you build the OData URI, “context.getServerUrl()” is deprecated as of CRM 2011 and it definitely does not work in CRM 2013. Use “Xrm.Page.context.getClientUrl()” instead. The “XMLHttpResponse” where the returned object is in JSON format (as set by the request header) looks like this:
Convert the text of the JSON object into JavaScript script by using “JSON.parse(…)” in order to make the result easier to manipulate.
JSON.parse(this.responseText).d
If you need more information on dealing with “XMLHttpRequest/Response” objects and what the “readystate” and status codes mean, try either the or the CRM 2013 SDK.
Let’s take a look at the actual JavaScript functions. Only relevant functions containing the Xrm methods mentioned above, are posted below. If you want to see the code working for your CRM 2013 environment, you will need to create your own JavaScript library with an unique namespace, and copy the functions below.
Check if “Parent Customer” field is set to blank or set to an existing account record.
If “Parent Customer” is set to an account record, asks the user if he wants the account’s address to be copied to the new contact record.
If “Parent Customer” is set to blank, asks the user if he wants the contact’s address to be deleted.
//Hook this method to the "OnChange" event of "parentcustomerid" lookup
endpoint security download endpoint security by bitdefender blocked this pageTAGS
CATEGORIES