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

How to Autofill Account Details and Contact Details with Javascript

(0) ShareShare
ReportReport
Posted on by 181

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);
      }
    );
  }

}

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

    Thank you! it worked.

  • 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;
                                }
                              
                              }

  • 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

  • Joseph Nasr Profile Picture
    181 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

    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

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,850 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,719 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans