How to Refresh Main Entity Views for an Entity in Dynamics 365

In Microsoft Dynamics 365, there may be an occasion that you need to refresh the Main view for an entity programmatically. For instance, if you are performing a custom process on a record from the view, and want the view to update automatically to reflect this as soon as the process has completed.

In this instance, as you are not on a form, there is no access to the Xrm.Page data client side functionality, which severely limits your ability to refresh data in a supported manner. The only supported way to do this currently is to force a page reload, which is not ideal if there is other contextual information on the page, or buttons that you want the user to be able to interact with, not to mention it breaks the flow of the application, which can prove to be annoying for users.

The other way to go about doing this is via direct DOM manipulation, which is of course, unsupported.
There are a couple of different ways to do this.

Option One

A similar data refresh effect can be accomplished by using some vanilla JavaScript and triggering the click event of the refresh button on the subgrid. This process can be broken down into a couple of steps:

image thumb How to Refresh Main Entity Views for an Entity in Dynamics 365

  1. Get the id of the refresh button. You can use the DOM Explorer built in to IE to accomplish this. Thankfully, this id (for me anyway), has always been ‘refreshButtonLink’, but it might pay to check this in case Microsoft changes this in the future.
  2. At the end of your custom code, call the following function:

image thumb 1 How to Refresh Main Entity Views for an Entity in Dynamics 365

That’s it. At this point, your custom code should refresh the view grid. I would recommend wrapping this functionality in a try/catch block in case something changes in the future, to prevent this from raising an error to the user.

Option Two

You can also get the grid element by use of JavaScript, and then call the refresh method of the grid control.

  1. Get the id of the grid. This should always be ‘crmGrid’, but again, in the future this may change.
  2. At the end of your custom code, call the following function:

image thumb 2 How to Refresh Main Entity Views for an Entity in Dynamics 365

Again, I would strongly suggest that if you decide to use this function, or a combination of the two, to wrap this in a try/catch block so that if anything does go wrong, an error is not presented to the user.

Let’s block ads! (Why?)

Magnetism Solutions Dynamics CRM Blog