Today we will be going over the steps how to set the value of a look up using JavaScript in Microsoft Dynamics CE (CRM).
I have created a custom table named as Student. It has the relation with the Contact table. The student table has Email field.
When I create new Student record and write the email of that student, it should automatically fill the Contact lookup by matching the contact with the same email.
How can I do that using JavaScript.
To do that I will create a Web source and attached the JavaScript file in which we can achieve that part.
function onChangeEmail(executionContext) {
var formContext = executionContext.getFormContext();
var emailElement = formContext.getAttribute("cr57c_email");
var emailValue = emailElement.getValue();
RetrieveContactRecord(formContext, emailValue);
} function RetrieveContactRecord(formContext, email) {
var url = formContext.getUrl().split('com/')[0] +"com";
var oDataUrl = url+"/api/data/v9.2/contacts?$filter=emailaddress1%20eq%20%27"+email+"%27&$select=contactid,yomifullname";
var retrieveReq = new XMLHttpRequest();
retrieveReq.open("GET", oDataUrl, false);
retrieveReq.setRequestHeader("Accept", "application/json");
retrieveReq.setRequestHeader("Content-Type", "application/json;charset=utf-8");
retrieveReq.onreadystatechange = function ()
{
if (retrieveReq.readyState == 4 && retrieveReq.status == 200)
{
var result = JSON.parse(retrieveReq.responseText);
if (result.value.length >= 1)
{
setContactOnStudent(formContext, result.value[0].contactid, result.value[0].yomifullname);
}
}
};
retrieveReq.send();
} function setContactOnStudent(formContext, contactid, name)
{
var lookupData = new Array();
var lookupItem = new Object();
lookupItem.entityType = "contact";
lookupItem.id = "{"+contactid+"}";
lookupItem.name = name;
lookupData[0] = lookupItem;
formContext.getAttribute("cr57c_contact").setValue(lookupData);
}
Thank you for reading this article hope you find it useful.
Technical Consultant
Microsoft Dynamics CRM is a powerful Customer Relationship Management (CRM) tool that helps businesses manage…
Today we will be going over the steps how to assign owner to specific Team…
In this article we will see how we can create and post free text invoice…
In this article we will be focusing on confirmation of a sales order using X++…
In this article we will be focusing on confirmation of a purchase order using X++…
In this article we will see how we can create inventory ownership change journal using…