Perform Asynchronous Operations with New Async OnLoad Event in Dynamics 365

Sending
User Review
0 (0 votes)

Introduction:

First, let us understand what is OnLoad event and where we use it.

It is an event which occurs after a form gets loaded or on the creation of a record. Generally, we use the OnLoad event to bind some data or hide/show fields, sections, tabs, etc. based on some conditions.

The behaviour of the OnLoad event is synchronous i.e., when you run an asynchronous code on load of a form and want to hide some fields based on the code response, the form will be loaded first with all the fields including the field you wanted to hide on load of the form. Later, when you receive the response of your code, it will hide the respective field. This is due to the synchronized nature of the OnLoad event. This leads to bad user experience, as the user who is not authorized to view the respective field will be able to view it on load of the form.

To overcome this limitation, Microsoft has introduced Async OnLoad event, which helps the user to run asynchronous code synchronously on load of the form. The AsyncOnLoad is by default not enabled in your environment.

How can we enable AsyncOnLoad in our environment?

To enable AsyncOnLoad please refer the steps given in this blog for enabling AsyncOnSave. Here, you will need to replace AsyncOnSave with AsyncOnLoad to enable the same. After enabling it in your environment, you will be able to use it.

Scenario:

To see it in action let us take a scenario where you want to hide the Details tab on load of the account form when the user does not have the sufficient or required security roles.

As I have explained above the synchronous nature of OnLoad event, if we don’t enable the Async OnLoad and perform our scenario then the Details tab will be visible on load. Only after receiving the response and checking the condition it will hide the Details tab. The result of this can be seen in the below gif. However, we want see the form UI as it will be shown after the response is received and not before i.e. Details tab already hidden on load. For that, we will need the new AsyncOnLoad event that will wait until it receives the response and then show the actual interface that the user is authorized to view.

Perform Asynchronous Operations with New Async OnLoad Event in Dynamics 365                                                                               Sync OnLoad

So let us check our code. Here, we have created an OnLoad function, which will get trigger on the load of the form.

this.onLoad = function (executionContext) {

    let functionName = “onLoad”;

    let formContext = executionContext.getFormContext();

    try {

      return new Promise((resolve, reject) => {

        fetchXml = “<fetch version=’1.0′ output-format=’xml-platform’ mapping=’logical’ distinct=’true’>” +

          “<entity name=’systemuser’>” +

          “<attribute name=’fullname’ />” +

          “<filter type=’and’>” +

          “<condition attribute=’systemuserid’ operator=’eq-userid’ />” +

          “</filter>” +

          “<link-entity name=’systemuserroles’ from=’systemuserid’ to=’systemuserid’ visible=’false’ intersect=’true’>” +

          “<link-entity name=’role’ from=’roleid’ to=’roleid’ alias=’ab’>” +

          “<filter type=’and’>” +

          “<condition attribute=’roleid’ operator=’eq’ uiname=’System Administrator’ uitype=’role’ value='{9D87E9D6-F1CA-EB11-8235-000D3AB031FA}’ />” +

          “<condition attribute=’roleid’ operator=’eq’ uiname=’Sales Manager’ uitype=’role’ value='{8E8B8843-6768-42CD-9D40-7EDD99BCE4A4}’ />” +

          “</filter>” +

          “</link-entity>” +

          “</link-entity>” +

          “</entity>” +

          “</fetch>”;

        let options = “?fetchXml=” + encodeURIComponent(fetchXml);

        parent.Xrm.WebApi.retrieveMultipleRecords(“systemuser”, options).then((response) => {

          if (response != null && response.entities != null && response.entities.length > 0)

            formContext.ui.tabs.get(“DETAILS_TAB”).setVisible(true);

          else

            formContext.ui.tabs.get(“DETAILS_TAB”).setVisible(false);

          resolve(“ok”);

        }).catch((error) => {

          reject(error);

        });

      });

    } catch (error) {

      console.log(functionName + “: ” + error.message);

    }

  }

We are checking two security roles System Administrator and Sales Manager, if the current user has both the roles then the Details tab will be visible or else it will get hidden.

Below is the result of how the user will see the form when AsyncOnLoad is enabled.

Perform Asynchronous Operations with New Async OnLoad Event in Dynamics 365

                                                                          Async OnLoad

Conclusion:

In this blog, we have learnt how to perform asynchronous code synchronously on load of the form by enabling AsyncOnLoad.

Click2Export