Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

How to Auto Fill Field on change of Lookup Field

Posted on by Microsoft Employee

Hi experts, 

I have added a Lookup field called "primary contact" in the project entity form, from which the user can select the contact. 

I have also added a field called "contact phone" which should fetch the phone number of the corresponding contact onchange of the lookup field. 

I have established this using workflows, however workflows do not trigger on change of the field so how can I accomplish this using javascript? Any sample code would be very helpful

Thanks!

Purvesh 

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to Auto Fill Field on change of Lookup Field

    Thanks so much!!

  • Verified answer
    Arpit Shrivastava Profile Picture
    Arpit Shrivastava 7,518 User Group Leader on at
    RE: How to Auto Fill Field on change of Lookup Field

    Hi Jon,

    You can put the fields separated by comma (,) like below:

    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/contacts()?$select=new_email,telephone1,firstname,fullname,gendercode", true);

    and can get the value like this:

    var email= result["new_email"]; 

    var email= result["firstname"]; 

     

    If you find the answer as useful, please mark it as verified

     

    Cheers
    Arpit
    https://arpitmscrmhunt.blogspot.in

     

     

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to Auto Fill Field on change of Lookup Field

    Hi Arpit,

    If I wanted to add another field to be pulled from the contact entity (e.g. new_email), what would I have to change?

    Thanks,

    Jon

  • Arpit Shrivastava Profile Picture
    Arpit Shrivastava 7,518 User Group Leader on at
    RE: How to Auto Fill Field on change of Lookup Field

    Hi Jon,

    I was about to respond you that the latest code I shared is working fine in my case.

    Always make sure, do Cntrl + F5 and refresh your browser before get the updated script changes onto the form.

    Anyways Cheers for the success.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to Auto Fill Field on change of Lookup Field

    Hi Arpit,

    It magically started working now!!

    Thanks a ton!

    Best,

    Jon

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to Auto Fill Field on change of Lookup Field

    I tried this code as well as it still gives me that same Script error that I shared in screenshot.

    Thanks so much for your help guys. Truly appreciate it!

    Jon

  • Verified answer
    Arpit Shrivastava Profile Picture
    Arpit Shrivastava 7,518 User Group Leader on at
    RE: How to Auto Fill Field on change of Lookup Field

    Please find the updated code;

    function getPhoneNumber()
    {
    var contactId;
    var res;
    var lookupItem = Xrm.Page.getAttribute("new_primarycontact ").getValue();
    if (lookupItem != null)
    {
    contactId = lookupItem[0].id;

    // Added new line
    res = contactId.replace("{", "").replace("}", "");
    }
    else
    {
    return;
    }
    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/contacts("+res+")?$select=telephone1", 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.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 address1_telephone1 = result["telephone1"];

    if(address1_telephone1)
    {
    Xrm.Page.getAttribute('new_telephone1').setValue(address1_telephone1);
    }
    }
    else
    {
    Xrm.Utility.alertDialog(this.statusText);
    }
    }
    };
    req.send();
    }

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: How to Auto Fill Field on change of Lookup Field

    Now you need to debug the code to see whats happeing.

    blogs.msdn.microsoft.com/.../debugging-custom-javascript-code-in-crm-using-browser-developer-tools

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to Auto Fill Field on change of Lookup Field

    Yes, I am selecting the contacts which contain data in the phone number fields in contact

  • Suggested answer
    Arpit Shrivastava Profile Picture
    Arpit Shrivastava 7,518 User Group Leader on at
    RE: How to Auto Fill Field on change of Lookup Field

    Make sure opted contact contains phonenumber

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans