Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 Community / Forums / Sales forum / How to Autofill Accoun...
Sales forum
Answered

How to Autofill Account Details and Contact Details with Javascript

Posted on by 146

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
    Joseph Nasr 146 on at
    RE: How to Autofill Account Details and Contact Details with Javascript

    Thank you! it worked.

  • Verified answer
    Leah Ju Profile Picture
    Leah Ju 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
    Bassey 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
    Joseph Nasr 146 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
    Bassey 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

Helpful resources

Quick Links

Dynamics 365 Community Update – Sep 16th

Welcome to the next edition of the Community Platform Update. This is a weekly…

Announcing Our 2024 Season 2 Super Users!

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

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,353 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,251 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans