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
Thanks so much!!
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
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
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.
Hi Arpit,
It magically started working now!!
Thanks a ton!
Best,
Jon
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
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();
}
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
Yes, I am selecting the contacts which contain data in the phone number fields in contact
Make sure opted contact contains phonenumber
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,253 Super User 2024 Season 2
Martin Dráb 230,188 Most Valuable Professional
nmaenpaa 101,156