How to execute a JavaScript function on all forms without registering it manually?

Model-driven apps provides a rich set of client APIs to interact with form objects and events to control what and when to display on a form. The JavaScript functions react to different events so that the code gets executed when the event occurs on the form.

The process of associating a function to an event is done manually for each form. We will see on this blog an interesting method to execute a JavaScript function on all the CRM forms without registering them manually.

Scenario: Open all lookups in a modal dialog across the CRM


Based on Microsoft’s recommendations, we can implement this scenario on a single form by registering the “onLoad” function on the “onLoad” event on the form account as an example:

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);
}
}
view raw AccountForm.js hosted with ❤ by GitHub

We will generalize the same feature across the CRM. To achieve this functionality, we will use the CRM ribbon. Indeed, we will create a dummy hidden button that will be placed on all CRM forms using the ribbon application, and we will inject our logic into a custom enable rule.

Ribbon Configuration:

Custom Enable Rule:

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);
}
}
view raw Utilities.js hosted with ❤ by GitHub

According to this approach, the function “MEA.Utilities.onLoad” will be executed each time a form is opened in the CRM. As a result, all lookups will be opened as a modal dialog across the CRM.

Demo:



Advertisement

4 thoughts on “How to execute a JavaScript function on all forms without registering it manually?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s