Stay on the same page when a lookup field is clicked in Dynamics 365 forms/ grids. Use the addOnLookupTagClick function

Follow Debajit’s Power Apps & Dynamics 365 Blog on WordPress.com

With every release of Dynamics 365, there is something always new for developers. In my last blog I explained about the enhancement in Xrm.WebApi.navigateTo method. Today we will discuss in detail about the new “addOnLookupTagClick” method introduced in Release wave 1, 2020.

So what does this function do? Actually it solves a long standing problem where clicking on a lookup value, it redirected to the lookup record form. A potential problem with this is the user would lose context of the record they are working on. But now this can be avoided. So let’s see how we can achieve the same.

For this demo I use the Account form, Parent Account (parentaccountid) field.

In the onload of the Account form, I add the below code.

Xrm.Page.getControl('parentcustomerid').addOnLookupTagClick(function(e){
e.getEventArgs().preventDefault(); // disable the default behavior which to open the lookup record.
// get the lookup record value
var lookupRecord = e.getEventArgs().getTagValue();
// get formContext
var fc = e.getFormContext();
});

The two most important part of the code are inside the function with comments. The first one is to prevent the default behavior which is navigating to the lookup record.

The next one is even more important which allows you to select the lookup record clicked. I used the developer tools to show you the format of data returned by function.

The final line of code is to get the form context.

So what actually we can achieve with this. Well a lot. You can open up the lookup record form in a dialog as I explained in my previous blog.

Or may be you can open up a webresource to show up anything. I mean anything that your customer need or want.

Debajit Dutta

(Dynamics MVP)

For consultation/ training visit www.xrmforyou.com or reach out to us at info@xrmforyou.com