User Review
( votes)Introduction:
In this blog, we will check how can we get the entity record information that is set in lookup control without losing the main entity form context.
Problem:
While we are on a record form of an entity, if we want to get the information about the record that is set in a lookup control within that record form, then we need to click on the record link on lookup control. When we click on the link of the lookup control, the respective set record gets opened in the same window and we lose the current entity from context. Let us take an example, suppose we open an Opportunity entity form that has an Account entity lookup control. We want to see the details about the respective account record that is set in lookup control, but the problem here is that when we click on lookup control we are redirected to the Account entity form of the respective set account record as shown in the below screenshot. In this case, we lose the main entity form context i.e Opportunity entity form.
Solution:
To overcome the context loss problem, Microsoft introduced one of the functions called addOnLookupTagClick() where we can get the information about the record which is set by the lookup control. This function will return the following parameters as follows:
1. id – It is a string type parameter that indicates the GUID of the record which is set in lookup control.
2. fieldName – It is a string type of parameter which contains the logical name of lookup control.
3. entityType – This parameter is also string type which indicates the entity logical name of the lookup control entity.
4. name – It indicates the name value of the record which is set in lookup control.
Please find the below source code which helps us to execute the addOnLookupTagClick() function to retrieve the information of the lookup record. When we click on lookup record, a CRM dialogue box will be opened which includes all the information related to the selected record as shown in the screenshot below.
Note: Below source code is registered on a load of Opportunity entity form.
function onLoadFunction (executionContext) { //retrive the form context var formContext = executionContext.getFormContext(); //Using addOnLookupTagClick Client API. formContext.getControl("parentaccountid").addOnLookupTagClick(function(e){ //retrict the lookup link click envent e.getEventArgs().preventDefault(); //get record details from addOnLookupTagClick success funtion var currentLookUpRecord = null; if(e.getEventArgs()._tagValue !=null && e.getEventArgs()._tagValue != undefined){ currentLookUpRecord = e.getEventArgs()._tagValue; } //display lookup record value using alert dialouge funtion var message = { confirmButtonLabel: "OK", text: "Name:"+ currentLookUpRecord.name+"\nId:"+currentLookUpRecord.id+"\nfieldName:"+currentLookUpRecord.fieldName+"\nentityType:"+currentLookUpRecord.entityType}; var alertOptions = { height: 200, width: 280 }; Xrm.Navigation.openAlertDialog(message, alertOptions).then( function success(result) { }, function (error) { console.log(error.message); } ); }); }
Conclusion:
With the help of addOnLookupTagClick() we can easily get the information of the respective record that is set in the lookup control without losing the main entity from context.