Open D365 Lookups in Modal Dialogs

Ever had the situation where you are opening a CRM record, and you keep digging into this record's connected entities. Whether that is an Account, for which you are looking the opportunity, for which you are looking the order, then the order line items, etc. At the end you barely remember where you started from, and then you need to go back to the view of records, or use the recent items section in the UCI menu. I'm sure we all still have this annoying process, which I personally find very time consuming.

There is a great new feature introduced in Dynamics 365 202 Wave 1 release that allows you to actually open a lookup record within the main record, for example open the Primary Contact record inside the Account record while the Account remains open in the background. Better see what I mean.

Here you can see that I'm looking a Contact record, within a Case, while the case is still open behind. Once I'm done with doing what i want to do with the Contact, all i need to do is to close it from the top right corner, and I'm still in the case. No further navigation, no need to use recent, and so on.


There are three methods you can achieve this and we will look into all three of them now. First of all i want to say HUGE thank you to Mehdi El Amri for spending some time with me and helping me on Method 2 and Method 3. This articles has been inspired by his original article How to execute a JavaScript function on all forms without registering it manually? – XRM Tricks (Power Platform & Dynamics CRM ) with his agreement, I will provide you here just some more instruction that are important to make it work. Make sure you've checked his blog where you will find some great solutions as well.

Method 1 Using modal dialog on a single filed.

This one is the standard method pointed by Microsoft, where you can using Modal Dialogs only for a certain filed.

  • You will need to create a JScript Web resource using this script.

function openAccountLookupModalDialog(executionContext) {

var formContext = executionContext.getFormContext(); // Getting FormContext

var lookupControl = formContext.getControl("parentcustomerid");

lookupControl.addOnLookupTagClick(onLookupClick);

}

function onLookupClick(executionContext) {

executionContext.getEventArgs().preventDefault();

var record = executionContext.getEventArgs().getTagValue();

Xrm.Navigation.navigateTo({

pageType: "entityrecord",

entityName: record.entityType,

entityId: record.id

}, {

target: 2, //2 - Open record in modal dialog

width:

{

value: 80,

unit: "%"

}

});

}

  • You need to change the var lookupControl = formContext.getControl("parentcustomerid"); with the schema name of the lookup filed which you want to open in a modal dialogs, in my case this is parentcustomerid.

  • Then you need to add this script to the desired entity form properties, in this example it is Account.

  • In the Event section, add the created JScript web resource, and also make sure you've add the same script to the Even Handlers section for OnLoad event.

  • Open the Event and add the following function: openAccountLookupModalDialog (the one on the screenshot is for Method 2. Make sure you've checked Enabled and Pass execution context as first parameter.

  • Save and Publish the changes, and then test the field if it opens in Modal Dialog.

Method 2 Using modal dialogs on the entire form

This method is the same as the Method 1, the only difference is in the JScript web resource code, where we don't mention a specific filed.

if (typeof (MEA) == "undefined") { MEA = {} };if (typeof (MEA.Account) == "undefined") { MEA.Account = {} };if (typeof (MEA.Account.AccountForm) == "undefined") { MEA.Account.AccountForm = {} };

MEA.Account.AccountForm = { onLoad: function (executionContext) { var formContext = executionContext.getFormContext(); var attributes = formContext.data.entity.attributes; attributes.forEach(att => { att.getAttributeType() == 'lookup' ? formContext.getControl(att.getName()).addOnLookupTagClick(this.openModalForm) : null }); }, openModalForm: function (executionContext) { var formContext = executionContext.getFormContext(); var record = executionContext.getEventArgs().getTagValue(); executionContext.getEventArgs().preventDefault(); var pageInput = {pageType: "entityrecord",entityName: record.entityType,entityId: record.id}; var navigationOptions = {target: 2,width:{value: 80,unit: "%"}}; Xrm.Navigation.navigateTo(pageInput,navigationOptions); }}

  • When you come to the step where you need to add the function to the Entity form OnLoad event, you can use this function MEA.Account.AccountForm.onLoad

  • Make sure you've checked Enabled and Pass execution context as first parameter. Save and Publish the changes, and then test the entire form if all lookups opens in Modal Dialog.

Method 3 Apply Modal Dialogs on every lookup in Dynamics CRM

This one is different, because as Mehdi says in the original article, you don't need to add the script to every form, instead we will create a dummy button on each form using Ribbon Workbench, which will be hidden, and which will execute the script on every form.

  • First, create a new JScript web resource with the following code

if (typeof (MEA) == "undefined") { MEA = {} };if (typeof (MEA.Utilities) == "undefined") { MEA.Utilities = {} };

MEA.Utilities = { onLoad:function(primaryControl, cmdProperties){ var formContext = primaryControl; this.openModalFormForAllLookupFields(formContext); }, openModalFormForAllLookupFields: function(formContext){ var attributes = formContext.data.entity.attributes; attributes.forEach(att => { att.getAttributeType() == 'lookup' ? formContext.getControl(att.getName()).addOnLookupTagClick(this.openModalForm) : null }); }, openModalForm: function (executionContext) { var formContext = executionContext.getFormContext(); var record = executionContext.getEventArgs().getTagValue(); executionContext.getEventArgs().preventDefault(); var pageInput = {pageType: "entityrecord",entityName: record.entityType,entityId: record.id}; var navigationOptions = {target: 2,width:{value: 80,unit: "%"}}; Xrm.Navigation.navigateTo(pageInput,navigationOptions); }}

  • Then, create a new solution and add only the following component into it: Application Ribbons

  • Finally, open Ribbon Workbench, and add a new button on the Form section.

  • Create new Enable Rule with the following parameters:

Default = False

FunctionName: MEA.Utilities.onLoad

Library: <the JScript web resource that you've created early>

CRM Parameter = Primary Control

CRM Parameter = CommandProperties

  • Create new dummy command.

  • Add a dummy Action for is, something like URL action to open a web page (remember that the button will be hidden)

  • Add the Enable Rule that you've created early

  • Return to the button main settings and add the Command and CommandCore values as shows on the screenshot at point 3.

  • Publish the ribbon changes and test the lookups from a different entities. You can also open a lookup within a lookup which will also open inside the old one, so they will layer one after another.

What is the minus on using Modal Dialogs?

There is only one thing that you won't be able to do once you've set Modal Dialogs (regardless of which method you've choose), you wont be able to open a lookup filed in a separate tab/screen as the rendering method is changed. You will need to remove the OnLoad event from the form (for method 1 and 2), or remove the button for method 3.

Good luck with exploring modal dialogs and feel free to reach me if you have any questions.

Stay Safe!

Recent Posts

See All