Aaron Turecki Aaron Turecki - 1 month ago 10
Javascript Question

crm 2011 js - Get associated grid view on refresh

I have a Kit quote product in CRM 2011 and I'm trying to call a function when the associated product view gets updated.

enter image description here

enter image description here

In the F12 debug tools I can see the grid id is 'crmGrid_productassociation_association':

enter image description here

But the following js code does not seem to work:

var grid = document.getElementById("crmGrid_productassociation_association").control;
if (grid != null)
{
alert("success");
}


And when I run this code on page load:

console.dir(Xrm.Page.ui.controls.get());


The crmGrid_productassociation_association control does not appear in the list of controls. Instead, only the controls on the originating quote product appear. Does anyone know how to call a js function on an associated grid view refresh?

Answer

For those of you wondering, I was able to get associated grids in CRM 2011 with JS, you need a function that looks like this:

function reloadKitProducts() {
    var iframe = document.getElementById('areaKitsFrame');

    if (iframe == null || iframe.readyState != "complete") {
        setTimeout(reloadKitProducts, 3000);
        return;
    }

    var subgrid = iframe.contentWindow.document.getElementById('crmGrid_productassociation_association').control.add_onRefresh(getKitProductsOnLineItemChange);
}

You need to get the id of the iframe that contains the subgrid. This is the main line here:

var subgrid = iframe.contentWindow.document.getElementById('crmGrid_productassociation_association').control.add_onRefresh(getKitProductsOnLineItemChange);

It only loads when you click the link in the navigation pane, and it will still take a second or two to load, so you have to implement a recursive timeout.