Working with the Web API

Another common use for JavaScript is to work with Dynamics 365 CE entity data. We can use the Web API, OData V4, which was introduced in Dynamics CRM 2016, or we can use Xrm.WebApi, which was introduced in Dynamics 365 V9. Let's say we have a requirement to copy an email from the primary contact in the customer form when the primary contact lookup is selected. Let's learn how to write JavaScript using both of these options. We can write these requests manually or we can use a community tool such as CRM REST Builder to write a Web API request. To use CRM REST Builder, follow these steps:

  1. Download the latest release of CRM REST Builder from https://github.com/jlattimer/CRMRESTBuilder/releases and import the solution into our organization.
  2. Once the solution has been imported, we can refresh our browser session and we will see a CRM Rest Builder button, which we can use to open CRM REST Builder.
  3. Once CRM REST Builder is open, we can simply generate our script using the following sequence:

Different methods are listed under Action, which we can use to write our Web API request.

  1. We can select our source entity from the Primary Entity drop-down list and we can select which entity field we want to retrieve in our request. We want to retrieve a contact record based on the contact's primary field (key), so we are using the Retrieve Single request.
  2. Once we have designed our request, we can click on the Create Request button to generate the request. It will open the Code tab with the request generated, which we can use in our JavaScript web resource.

We are going to use our existing account library file and add a method that will trigger when the primary contact changes. The following script is used to populate the email field of the account entity form once the primary contact field has been selected:

this.primarContactOnChange = function(executionContext) {
var formContext = executionContext.getFormContext();
//get global context
var globalContext = Xrm.Utility.getGlobalContext();
//get primary contact id
if (formContext.getAttribute("primarycontactid") != null &&
formContext.getAttribute("primarycontactid").getValue() != null) {
var contactid = formContext.getAttribute("primarycontactid").getValue()[0].id.substring(1, 37);
//prepare request
var req = new XMLHttpRequest();
req.open("GET", globalContext.getClientUrl()+ "/api/data/v9.1/contacts(" +
contactid + ")?$select=emailaddress1", true);
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.onreadystatechange = function() {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var result = JSON.parse(this.response);
var emailaddress1 = result["emailaddress1"];
if (emailaddress1 != null)
formContext.getAttribute("emailaddress1").setValue(emailaddress1);
} else {
console.log(this.statusText);
}
}
};
req.send();
}
}

In the preceding code, first, we get the GUID from the primary contact field. Once we have the primary contact ID, we can simply retrieve the contact's email ID using the following syntax:

req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/contacts(" + contactid + ")?$select=emailaddress1", true);

After that, we prepare the request head, which is almost the same for every request, and then we check whether the execution is completed and that an OK status has been returned. We do this using the following syntax:

if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
//code to process result
}}

We can call our HIMBAP .primarContactOnChange method on OnChange of the primary contact lookup, just like we added an OnChange event for the Relationship type field. Similarly, we can generate an Xrm.WebAPI request by clicking on the Xrm.WebApi button under the Output folder. The following code is the Xrm.WebApi request to get the email ID:

this.primarContactOnChange = function(executionContext) {
var formContext = executionContext.getFormContext();
//get entity id
if (formContext.getAttribute("primarycontactid") != null && formContext.getAttribute("primarycontactid").getValue() != null) {
var contactid = formContext.getAttribute("primarycontactid").getValue()[0].id.substring(1, 37);
Xrm.WebApi.online.retrieveRecord("contact", contactid, "?$select=emailaddress1").then(
function success(result) {
var emailaddress1 = result["emailaddress1"];
formContext.getAttribute("emailaddress1").setValue(emailaddress1);
},
function(error) {
console.log(error.message);
}
);
}
}

Now we know how we can use client-side requests to work with entity data. So far, we've discussed how to query data in traditional ways using Web API requests and how to use Xrm.Webapi to get data from an entity. Now, let's discuss what options are available for working with an entity and its data from the server-side.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.45.137