web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

auto populate custom entity using java script without knowing the guid value of the parent entity

(0) ShareShare
ReportReport
Posted on by

Hi I just started to use client side scripting, although it might me those another goto goggle 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,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 dynamical retrival of guid value and autopopulating 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["cts_zipcode@OData.Community.Display.V1.FormattedValue"];

} else {

Xrm.Utility.alertDialog(this.statusText);

}

}

};

req.send();

hope i am able to give a detailed overview of my problem.Thanks in advance for going through it, again sorry to post another goto google search problem.believe me i tried to find it before posting it. thanks :)

I have the same question (0)
  • cloflyMao Profile Picture
    25,210 on at

    Hi Partner,

    Based on your two blockage, I want to confirm more details:

    1.The dynamical retrieval of guid value

    I am not clear why the "agent id" field of cts_case is whole number type field and is mapped to cts_agent entity.

    As per my understanding, would it be possible that there is also a whole number field for cts_agent entity to save the mapping relationship?

    For example,

    There is a cts_case record and data of two fields of it are:

    name: Hello CRM

    cts_agentid: 1234567

    At same time, there is a cts_agent record and data of two fields of it are:

    name: CRM agent

    cts_agentid: 1234567

    Therefore, you want to use cts_agentid of cts_case entity to retrieve guid id of corresponding cts_agent record.

    Would example I gave be your situation?

    2. Autopopulating the cts_case with JSON

    From your sample code, you have retrieved data from corresponding cts_agent,

    so you just need to run the code at OnLoad event of cts_case form,

    and use formContext.getAttribute("xxx").setValue(cts_xxx) function to populate fields of the cts_case form.

    Regards,

    Clofly

  • Community Member Profile Picture
    on at

    Clofly you are absolutely right cts_agent id contains in both the forms, although there is no name, although I am unable to retrieve the guid id using javascript dynamically, which could be done easily using plugin. the problem is that I want to trigger my plugin OnChange cts_agentid, and the process flow will be as such

    OnChange(cts_agentId{cts_case})-->dynamically retrieve the record from cts_agent form(guid)-->populate the retrieved date in cts_case form

    even I thought why it needs the whole number but its according to srs docx . Thanks for understanding, I am still tucked there. s

  • Verified answer
    cloflyMao Profile Picture
    25,210 on at

    Hi Partner,

    If the field is whole number, you could take it as filter criteria to retrieve the corresponding cts_agent, and use retrieveMultipleRecords function of client API to simplify the retrieval request.

    Run the sample autoPopulate function at OnChange event of cts_agentId(cts_case) field.

    function autoPopulate(executionContext) {
      var formContext = executionContext.getFormContext();
    
      // Get value of the agentid field of cts_case form
      var agentIdNumber = formContext.getAttribute("cst_agentid").getValue();
    
      // Retrieve corresponding cts_agent record by taking the whole number value as filter criteria
      Xrm.WebApi.retrieveMultipleRecords("cts_agent", "?$filter=cts_agentnumberid eq "   agentIdNumber   "").then(
        function success(result) {
          if (result.entities.length > 0) {
            var agentname = result.entities[0]['name'];
            formContext.getAttribute("cst_casename").setValue(agentname);
          } else {
            console.log("No value");
          }
        },
        function (error) {
          console.log(error.message);
        }
      );
    }
    

    Regards,

    Clofly

  • Community Member Profile Picture
    on at

    Hi Clofly ,

    you are absolutely right , even i changed my course to xrm.WebApi for simplification , I should have searched Microsoft documents more before posting my query, hope this thread will be a learners point for future dynamics developers and to overcome my blockage i am providing with the links where i commit  my mistake

    1. Microsoft have decrepit Xrm.page and will stop support for the same in future(my op was about that)

    docs.microsoft.com/.../important-changes-coming

    2.I was querying wrong data instead in my op and also was not aware of srm webApi to get started with it this link will help a lot

    docs.microsoft.com/.../retrievemultiplerecords

    Thanks Clofly  for being an active community member , keep growing microsoft dynamics community in future.

    Regards,

    Subhajit

  • cloflyMao Profile Picture
    25,210 on at

    Hi Subhajit,

    Thanks for sharing your experience, welcome to join Dynamics community.

    Regards,

    Clofly

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 108 Super User 2025 Season 2

#2
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 49 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans