web
You’re offline. This is a read only version of the page.
close
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 Auto Fill Field on change of Lookup Field

(0) ShareShare
ReportReport
Posted on by

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

I have the same question (0)
  • Suggested answer
    Ryan Maclean Profile Picture
    3,070 on at

    Hi Purvesh,

    It sounds like using a Quick View form would be better for you in this scenario: technet.microsoft.com/.../dn531145.aspx

  • Community Member Profile Picture
    on at

    Hi Ryan,

    I can't use a quick view form for this requirement. I need the phone number field to auto populate as soon as the user selects a value from the lookup field.

  • Suggested answer
    Community Member Profile Picture
    on at

    Hi,

    here is an example: community.dynamics.com/.../255317

    and here the crmRestuilder to build your request: github.com/.../releases

  • Ryan Maclean Profile Picture
    3,070 on at

    What is the problem with your workflow? Can you post a picture of the workflow to let us see the logic you're using?

  • Suggested answer
    nfernandezba Profile Picture
    210 on at

    You can use a Calculated field for the "contact phone" field. It will automatically update when you change the contact, and you won't need any code.

  • Arpit Shrivastava Profile Picture
    7,518 User Group Leader on at

    Hi Jon,

    Here is the code, run it on onchange event of primarycontact field.

    function getPhoneNumber(){

    var contactId;
    var lookupItem = Xrm.Page.getAttribute("Lookup_primarycontactfieldname").getValue();
    if (lookupItem != null)
    {
    contactId = lookupItem[0].id;
    }
    else
    {
    return;
    }

    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/contacts("+contactId +")?$select=Contact_phonenumberfieldname", 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["Contact_phonenumberfieldname"];

    if(address1_telephone1){

    Xrm.Page.getAttribute('phonenumber_Text_Field_where_you_want_to_setvalue').setValue(address1_telephone1);

    }

    } else {
    Xrm.Utility.alertDialog(this.statusText);
    }
    }
    };
    req.send();
    }

    Mark this an answer, If it helps.

    Cheers
    Arpit
    https://arpitmscrmhunt.blogspot.in

  • Community Member Profile Picture
    on at

    Hi Arpit,

    I'm getting the following error on the Script. Would could be wrong?

    Screen-Shot-2018_2D00_02_2D00_20-at-11.51.59-AM.png

    Thanks,

    Jon

  • Arpit Shrivastava Profile Picture
    7,518 User Group Leader on at

    Did you properly replace the field name (in place of text highlighted in Red)?

    Regards

    Arpit

  • Community Member Profile Picture
    on at

    Yeap, I think so

    Lookup_new_primary contact is the lookup on the project entity form

    Contact_telephone1 is the phone number field in the contact entity form 

    phonenumber_new_telephone1 is the phone number field in the project entity form

    Please suggest. I'm sure I'm going wrong somewhere. 

    function getPhoneNumber()
    {
    	var contactId;
    	var lookupItem = Xrm.Page.getAttribute("Lookup_new_primarycontact").getValue();
    	if (lookupItem != null)
    	{
    		contactId = lookupItem[0].id;
    	}
    	else
    	{
    		return;
    	}
    
    	var req = new XMLHttpRequest();
    	req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/contacts("+contactId +")?$select=Contact_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["Contact_telephone1"];
    				if(address1_telephone1)
    				{
    					Xrm.Page.getAttribute('phonenumber_new_telephone1').setValue(address1_telephone1);
    				}
    			} 
    			else 
    			{
    				Xrm.Utility.alertDialog(this.statusText);
    			}
    		}
    	};
    	req.send(); 
    }


    Thanks!

    Jon

  • Suggested answer
    Community Member Profile Picture
    on at

    Hi,

    before accesing directly the getValue of

    var lookupItem = Xrm.Page.getAttribute("Lookup_new_primarycontact").getValue();

    you should first check if  Xrm.Page.getAttribute("Lookup_new_primarycontact") is not null or empty

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans