Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to autofill a field based on another selection?

(0) ShareShare
ReportReport
Posted on by

In my Opportunity form, I want to lookup a Contact entity and then have the contact's email address also populate in the Opportunity form based on the email address that is in the Contact entity record. How can I set this up so that when the Contact is selected, the email address field automatically populates in real time? (Also, I have little experience writing code but am willing to give it a try. Everything I've read so far has been too complex from a coding side, like this article: community.dynamics.com/.../odata-with-json-getting-related-values-from-a-lookup-field-in-crm-2011)

*This post is locked for comments

  • Suggested answer
    Aric Levin Profile Picture
    30,188 Moderator on at
    RE: How to autofill a field based on another selection?

    You can use JavaScript and WebApi to retrieve the email address of the contact record and populate another address on the change event.

    function contactOnChange()

    {

      var contact = Xrm.Page.getAttribute("new_contactid").getValue();

      var contactId = contact[0].id.toString();

      contactId = contactId .replace(/[{}]/g, "");

    var req = new XMLHttpRequest();

    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/contacts(" + contactId + ")?$select=emailaddress1", false);

    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.onreadystatechange = function() {

       if (this.readyState === 4) {

           req.onreadystatechange = null;

           if (this.status === 200) {

               var result = JSON.parse(this.response);

               var emailaddress1 = result["emailaddress1"];

               Xrm.Page.getAttribute("new_emailaddress"].setValue(emailAddress1);

           } else {

               Xrm.Utility.alertDialog(this.statusText);

           }

       }

    };

    req.send();

    }

    NOTE: You should familiarize yourself with CRM Rest Builder by Jason Lattimer. Download it, install it in your environment, and it will help you with generating web api calls within your CRM environment,

    Hope this helps. Good luck.

  • Suggested answer
    Syed Ibrahim Profile Picture
    6,257 on at
    RE: How to autofill a field based on another selection?

    You can retrieve related entity record and its fields using web api (js) and set the value on change of that lookup field. Refer below how to retrieve record using web api.

    [View:https://parthimscrm.wordpress.com/2017/04/19/retrieve-lookup-option-set-value-from-web-api-mscrm/]

    [View:http://www.inogic.com/blog/2016/02/set-values-of-all-data-types-using-web-api-in-dynamics-crm/]

    Hope this helps.If the suggestion resolved your query .Pls mark it as answered/verified to close the thread.

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,865 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,723 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans