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

743 Views Asked by At

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();
2

There are 2 best solutions below

4
On BEST ANSWER

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

0
On

the dynamic retrieval of guid value and auto-populating the cts_cases with json are my two blockage I am facing

You got assistance from another answer on the first part. Like explained, you will be able to query the entity record with any attribute value (unique) you know of.

For second part - once you get the guid, you will be able to pass that as a parameter to Xrm.Navigation.openForm which can be used to open cts_cases entity form, and on form load using this parameter - pull the details from agent record and assign the attribute values from json to the opened cts_cases form fields. Or you can pass the values (in case of minimal attributes) as parameters too while opening the form. Read more

var entityFormOptions = {};
entityFormOptions["entityName"] = "cts_cases";

// Set default values for the Contact form
var formParameters = {};
formParameters["cts_addressline1"] = result["cts_addressline1"]
//other attributes go here

// Open the form.
Xrm.Navigation.openForm(entityFormOptions, formParameters).then(
    function (success) {
        console.log(success);
    },
    function (error) {
        console.log(error);
    });