Search code examples
javascriptjsondynamics-crmcustomizationdynamics-crm-webapi

Auto-populate custom entity using javascript without knowing the guid value of the parent entity in Dynamics 365


I just started to use client side scripting, although it might me those another go to google search community question, but believe me I have scourged google and communities but to link my query into a single unit always fail, let me describe the problem statement to give you a better idea.

I have created two custom entity named cts_agent and cts_cases, now I need to auto populate all the fields of cts_cases which are read only property fields except one which is agent id (whole number) which is mapped to cts_agent entity form.

If it have been an entity reference field I could use the query expression to fetch the details from the agents_form and auto populate the details in my cts_form but I need to write a js query which could take the agent id and fetch me those details. After fetching the details will be presented in a json format from where I need to populate the details in my cts_cases form which is also another problem I am unable to address, the dynamic retrieval of guid value and auto-populating the cts_cases with json are my two blockage I am facing. I have written a code for static version though:

var req = new XMLHttpRequest();

req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/cts_agents(00000000-0000-0000-0000-000000000000)?$select=cts_addressline1,cts_addressline2,cts_addressline3,cts_city,cts_country,cts_email,cts_fax,cts_mobilenumber,cts_name,cts_phonenumber,cts_state,cts_zipcode", 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 result = JSON.parse(this.response);

        var cts_addressline1 = result["cts_addressline1"];

        var cts_addressline2 = result["cts_addressline2"];

        var cts_addressline3 = result["cts_addressline3"];

        var cts_city = result["cts_city"];

        var cts_country = result["cts_country"];

        var cts_email = result["cts_email"];

        var cts_fax = result["cts_fax"];

        var cts_mobilenumber = result["cts_mobilenumber"];

        var cts_name = result["cts_name"];

        var cts_phonenumber = result["cts_phonenumber"];

        var cts_state = result["cts_state"];

        var cts_zipcode = result["cts_zipcode"];

        var cts_zipcode_formatted = result["[email protected]"];

    } else {

        Xrm.Utility.alertDialog(this.statusText);

    }

}

};

req.send();

Solution

  • you can configure a filter in the arguments. you can also use the available Xrm.WebApi functions for simpler querying syntax

    $filter=agentid eq 123
    //where agentid is your integer identifier field
    

    Example:

    Xrm.WebApi.retrieveMultipleRecords("cts_agent","?$select=cts_addressline1,cts_addressline2,cts_addressline3,cts_city,cts_country,cts_email,cts_fax,cts_mobilenumber,cts_name,cts_phonenumber,cts_state,cts_zipcode&$filter=agentid eq 123")
    .then(
    result => {
    //you read the results here - retrieve multiple returns an array of records.
    if(result.entities.length > 0)
    {
    var cts_addressline1 = result.entities[0].cts_addressline1;
    //etc...
    }
    },
    err => {
    console.error(err);
    });
    

    if you want the agent unique id (guid) you can just get it from result.entities[0].cts_agentid