Dynamics CRM 2011 · Javascript

Auto refresh / reload subgrid on other subgrid refresh

I come across this indirect requirements rather often in CRM projects. Say you have two subgrids in a form that are interlinked to each other. So when a new record is added to subgrid A; the items in subgrid B will adjust accordingly. Sure, you can tell users to click subgrid B’s refresh button, but wouldn’t it be neat if subgrid B automatically refreshes when subgrid A changes. Well, here’s how.

To refresh subgrid:

var subgrid = Xrm.Page.ui.controls.get("<subgrid name>");
subgrid.refresh();

To setup auto refresh subgrid on other subgrid refresh:

function Form_OnLoad() {
  setTimeout(subGridA_OnLoad, 1000); //We're using setTimeout() here because subgrids are loaded after $(document) is ready.
}

function subGridA_OnLoad() {
  var subgridA = document.getElementById("subgridA");
  subgridA.attachEvent("onrefresh", RefreshSubgridB);
}

function RefreshSubgridB() {
  var subgridB = Xrm.Page.ui.controls.get("subgridB");
  subgridB.refresh();
}

Cheers – Sy

Advertisements

4 thoughts on “Auto refresh / reload subgrid on other subgrid refresh

  1. Are there also other events I can attach on the subgrid (e.g. OnRecordnserted, OnRecordDelete) sometimes the “onrefresh” event gets triggered during the first load of the form and this ends in an endless loop. Hope for your help.

    Best regards from Germany

    1. Hi Kai,

      I can’t be certain about other events for the subgrid, but if I have to make a best guess there isn’t any, or if there is it will be unsupported. In saying that, the ribbon changes to subgrid menu when you select the subgrid. So you can attach javascript functions on record association, crud events with custom actions, and also display and enable rules on the ribbon menus.

      The ‘onrefresh’ event does sometimes end up in infinite loop depending on browser version, etc. How I handled this is first to check the IE version and disable autorefresh if not supported properly and finally using javascript window variable to determine refresh after the page is loaded.

      Hope that helps and let me know how you go.

      Sy

  2. Hi silong,

    function SubgridAttachEventRowInserted()
    {
    debugger;

    var gridControl = document.getElementById(“Questions”);
    if (gridControl == null || gridControl.readyState != “complete”)
    {

    setTimeout(‘SubgridAttachEventRowInserted()’, 1000);
    return;
    }
    //gridControl.control.refresh();
    gridControl.attachEvent(“onrefresh”, choicese)

    }

    function choicese()
    {
    //debugger;

    var gridControl = document.getElementById(“Choices”);
    if (gridControl == null || gridControl.readyState != “complete”)
    {
    //The subgrid hasn’t loaded, wait 1 second and then try again
    setTimeout(‘choicese()’, 1000);
    return;
    }
    gridControl.refresh();
    //gridControl.attachEvent(“onrefresh”, gridrowinsert)

    }

    I am trying to refresh one subgrid on base of another subgrid but it is not getting refreshed.

    1. Hi Amit,

      One important part of auto-refresh is attachEvent to the gridControl. Here I used setTimeout to make sure the gridControls are loaded and I used 1000ms. I would suggest to increase this to 3000ms to make sure everything is loaded. I noticed that your code have a timed-out recursive call, I’d avoid this first and make sure that the attachEvent is called properly.

      You then can inspect the event on the gridControl to see whether the attachEvent is updated properly.

      Also, try the gridControl.refresh function manually to see if it works properly.

      What update is your CRM on?

      Let me know how you go.

      Sy

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 )

Google+ photo

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

Connecting to %s