No module Published on Offcanvas position

CRM Dynamics 365 WebAPI and Javascript

When you need to access data that's in a different entity because it's in a lookup field for example, you could use Business Rules but to a certain extend. But what if you want or NEED to use javascript?
This is where the API for CRM Dynamics 365 comes in handy! You can write the code all by yourself using XMLHttprequest, but if you are like me not being a dedicated coder you tend to forget how to do it because you haven't done it in a long time.

For anyone that is like me, is lazy or doesn't have any experience in writing scripts, lucky for us there is CRM REST Builder by jlattimer! It gives CRM administrators one of the easiest ways to create a working piece of javascript code to update, retrieve or create records!
You can download it here: https://github.com/jlattimer/CRMRESTBuilder/releases
Once downloaded, you can import the solution into your CRM Dynamics instance. Afterwards refresh your page so that the CRM REST Builder button will appear in the ribbon.

crmrestbuilder

Endpoint:
I use the Web API endpoint myself in combination with the 9.1 version. I havn't encountered a reason yet to go to 2011 or a lower version.

Action:
The Actions are self-explanatory. Retrieving a single record requires you to enter the GUID of the record.
Even when you only need 1 result, use Retrieve Multiple to find it. Like using Advanced Find in CRM you can search attibutes to find your record.

Output Format
I've been using XMLHTTP as that works for me, but jQuery is also a good option as a alternative to get/enter your data.

When retrieving multiple records, you can select under Fields to Select which fields you want to show in the results. If you don't, it will show every attribute within that entity.

Once you click Create Request it will bring you to to a new page with several tabs.
Both Code (Read-Only) and Code (Editor) contain the code you need for your javascript. You can use the editor to "test" things that doesn't require the functionality of CRM, like a simple alert box.

To see if the code actually works and check if the output is what you expect, click the Execute Code (Read-Only) button.
Once you've done that, the retrieved record will show in the Results tab.

To test the code in CRM Dynamics, create a new javascript webresource and then create a simple function and paste the code in the function making it look something like this:

 
function test() {
    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/accounts?$filter=contains(name, 'test')", 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.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
    req.onreadystatechange = function() {
    if (this.readyState === 4) {
        req.onreadystatechange = null;
    if (this.status === 200) {
        var results = JSON.parse(this.response);
        for (var i = 0; i < results.value.length; i++) {
        var accountid = results.value[i]["accountid"];
        alert(accountid);
    }
    } else {
        Xrm.Utility.alertDialog(this.statusText);
    }
    }
    };
    req.send();
}

In this example the script will pull data out the Accounts entity and looking for records that contain the name "Test", afterwards it will throw a alert popup with the GUID of the record.
You can basically replace the alert part with something like formcontext.getAttribute("fieldname").setValue("value") to populate a simple text field with the GUID for example or anything else you can retrieve from that entity!

Using this it's also possible to dynamically populate a lookup field. Let's say the Owner field of a record based on the region field in the Account entity.
Have the WebAPI lookup the systemuser entity looking for records that match the same region value in the Account entity, retrieve the GUID and populate the Owner field with this GUID.
The code to populate lookup fields can be found in this article Populating Lookup Fields using Javascript
All you need to do is combine both codes in one single function. Here's a hint: Start populating the lookup field AFTER you've retrieved the GUID.