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

Community site session details

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

How to Autofill Account Details and Contact Details with Javascript

(0) ShareShare
ReportReport
Posted on by 183

Hello, 

In the prospect entity I have 2 lookup fields Client (which gets records from 'Account') and Contact (the contact of that Account)

8233.Capture.PNG

The fields that are filled when a Client is selected are auto filling normally, including contact. However the fields related to Contact (Contact's E-mail, Contact's Phone #, Contact's Mobile #) are not being auto filled until I save and refresh. Is there a way to overcome this and let them autofill? 

Code to autofill Client (Account) Details:

function setClientInfo(executionContext) {
  debugger;
  var formContext = executionContext.getFormContext();
  var selectedClientControl = Xrm.Page.getControl("new_ti_clientaccount");
  var selectedClient = selectedClientControl.getAttribute().getValue();

  var clientAddressControl = Xrm.Page.getControl("new_ti_clientaddress");
  var clientWebsiteControl = Xrm.Page.getControl("websiteurl");
  var clientContactControl = Xrm.Page.getControl("parentcontactid");

  if (selectedClient != null) {
    var selectedClientID = selectedClient[0].id;
    Xrm.WebApi.retrieveRecord("account", String(selectedClientID), "?$select=websiteurl,new_ti_headofficeaddress,_primarycontactid_value").then(
      function success(result) {
        console.log(result);
        // Columns
        var accountid = result["accountid"]; // Guid
        var websiteurl = result["websiteurl"]; // Text
        var new_ti_headofficeaddress = result["new_ti_headofficeaddress"]; // Multiline Text
        var primarycontactid = result["_primarycontactid_value"]; // Lookup
        var primarycontactid_formatted = result["_primarycontactid_value@OData.Community.Display.V1.FormattedValue"];
        var primarycontactid_lookuplogicalname = result["_primarycontactid_value@Microsoft.Dynamics.CRM.lookuplogicalname"];

        clientAddressControl.getAttribute().setValue(result.new_ti_headofficeaddress);
        clientWebsiteControl.getAttribute().setValue(websiteurl);
        clientContactControl.getAttribute().setValue([{ id: primarycontactid, name: primarycontactid_formatted, entityType: primarycontactid_lookuplogicalname }]);

      },
      function (error) {
        console.log(error.message);
      }
    );
  } else {
    return;
  }

}

 

Code to autofill Contact Details Once contact is filled:

function setClientContactInfo (executionContext){
  debugger;
  var formContext = executionContext.getFormContext();

  var selectedClientContactControl = Xrm.Page.getControl("new_ti_clientaccount");
  var selectedClientContact = selectedClientContactControl.getAttribute().getValue();
  
  var clientContactEmailControl = Xrm.Page.getControl("emailaddress1");
  var clientContactPhoneControl = Xrm.Page.getControl("new_ti_phonenumber");
  var clientContactMobileControl = Xrm.Page.getControl("mobilephone");

  if(selectedClientContact != null){
    var selectedClientContactID = selectedClientContact[0].id;
    Xrm.WebApi.retrieveRecord("contact", String(selectedClientContactID), "?$select=emailaddress1,new_ti_phonenumbercontact,mobilephone").then(
      function success(result) {
        console.log(result);
        // Columns
        var contactid = result["contactid"]; // Guid
        var emailaddress1 = result["emailaddress1"]; // Text
        var new_ti_phonenumbercontact = result["new_ti_phonenumbercontact"]; // Text
        var mobilephone = result["mobilephone"]; // Text


        clientContactEmailControl.getAttribute().setValue(emailaddress1);
        clientContactPhoneControl.getAttribute().setValue(new_ti_phonenumbercontact);
        clientContactMobileControl.getAttribute().setValue(mobilephone);

      },
      function(error) {
        console.log(error.message);
      }
    );
  }

}

I have the same question (0)
  • Bassey Profile Picture
    9 on at
    RE: How to Autofill Account Details and Contact Details with Javascript

    Hi Joseph Nasr

    I am not a developer, so I might not understand your code; however, I think you have the javascript configured on an Onload event. This documentation might help provide some context - learn.microsoft.com/.../form-onload

  • Joseph Nasr Profile Picture
    183 on at
    RE: How to Autofill Account Details and Contact Details with Javascript

    Hey Bassey thanks for your reply. No I actually have the second code trigger when 'Contact' field changes (onChange)

  • Bassey Profile Picture
    9 on at
    RE: How to Autofill Account Details and Contact Details with Javascript

    Alright Joseph, perhaps this might help - https://learn.microsoft.com/en-us/power-apps/developer/model-driven-apps/clientapi/reference/events/attribute-onchange

    pastedimage1684157792737v1.png

  • Verified answer
    Leah Ju Profile Picture
    Microsoft Employee on at
    RE: How to Autofill Account Details and Contact Details with Javascript

    Hi Joseph,

    My suggestion is to populate all fields directly from the Client fields.

    If the contact to be populated is always from an account, you can combine those two codes into one.

    In your first js code, you have get contact ID, Right?

    pastedimage1684202647290v1.png

    Then you can use the variable as guid to retrieve contact:

    Xrm.WebApi.retrieveRecord("contact", String(primarycontactid), "?$select=emailaddress1,new_ti_phonenumbercontact,mobilephone").then(..................

    Like this:

                            function setClientInfo(executionContext) {
                                debugger;
                                var formContext = executionContext.getFormContext();
                                var selectedClientControl = Xrm.Page.getControl("new_ti_clientaccount");
                                var selectedClient = selectedClientControl.getAttribute().getValue();
                              
                                var clientAddressControl = Xrm.Page.getControl("new_ti_clientaddress");
                                var clientWebsiteControl = Xrm.Page.getControl("websiteurl");
                                var clientContactControl = Xrm.Page.getControl("parentcontactid");
                              
                                if (selectedClient != null) {
                                  var selectedClientID = selectedClient[0].id;
                                  Xrm.WebApi.retrieveRecord("account", String(selectedClientID), "?$select=websiteurl,new_ti_headofficeaddress,_primarycontactid_value").then(
                                    function success(result) {
                                      console.log(result);
                                      // Columns
                                      var accountid = result["accountid"]; // Guid
                                      var websiteurl = result["websiteurl"]; // Text
                                      var new_ti_headofficeaddress = result["new_ti_headofficeaddress"]; // Multiline Text
                                      var primarycontactid = result["_primarycontactid_value"]; // Lookup
                                      var primarycontactid_formatted = result["_primarycontactid_value@OData.Community.Display.V1.FormattedValue"];
                                      var primarycontactid_lookuplogicalname = result["_primarycontactid_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
                              
                                      clientAddressControl.getAttribute().setValue(result.new_ti_headofficeaddress);
                                      clientWebsiteControl.getAttribute().setValue(websiteurl);
                                      clientContactControl.getAttribute().setValue([{ id: primarycontactid, name: primarycontactid_formatted, entityType: primarycontactid_lookuplogicalname }]);
                                      //retrieve contact
                                      Xrm.WebApi.retrieveRecord("contact", String(primarycontactid), "?$select=emailaddress1,new_ti_phonenumbercontact,mobilephone").then(
                                        function success(result) {
                                          console.log(result);
                                          // Columns
                                          var contactid = result["contactid"]; // Guid
                                          var emailaddress1 = result["emailaddress1"]; // Text
                                          var new_ti_phonenumbercontact = result["new_ti_phonenumbercontact"]; // Text
                                          var mobilephone = result["mobilephone"]; // Text
                                  
                                  
                                          clientContactEmailControl.getAttribute().setValue(emailaddress1);
                                          clientContactPhoneControl.getAttribute().setValue(new_ti_phonenumbercontact);
                                          clientContactMobileControl.getAttribute().setValue(mobilephone);
                                  
                                        },
                                        function(error) {
                                          console.log(error.message);
                                        }
                                      );
                              
                                    },
                                    function (error) {
                                      console.log(error.message);
                                    }
                                  );
                                } else {
                                  return;
                                }
                              
                              }

  • Joseph Nasr Profile Picture
    183 on at
    RE: How to Autofill Account Details and Contact Details with Javascript

    Thank you! it worked.

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
MVP-Daniyal Khaleel Profile Picture

MVP-Daniyal Khaleel 125

#2
Tom_Gioielli Profile Picture

Tom_Gioielli 110 Super User 2025 Season 2

#3
Erin Lubben Profile Picture

Erin Lubben 73

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans