0

As an JScript newbie, I have a problem with a subgrid in MS CRM 2011.

I have a form with a subgrid and in OnSave of that form, I want to loop over all the rows in the subgrid.

How can I do this with JScript ? Or is it possible another way, ex plugin ?

Thx

Matt
  • 4,656
  • 1
  • 22
  • 32
Robby Iven
  • 51
  • 1
  • 1
  • 3

4 Answers4

1

Here is the sample code which you can do on save of the form

var gridControl = document.getElementById('grdrelatedcontacts').control;
for (var intRowNumber = 0; intRowNumber < gridControl.getRecordsFromInnerGrid().length; intRowNumber++)
    for (var intCellNumber = 0; intCellNumber < gridControl.getRecordsFromInnerGrid()[intRowNumber][3].cells.length; intCellNumber++)
        alert(gridControl.getRecordsFromInnerGrid()[intRowNumber][3].cells[intCellNumber].outerText);
Asim Sajjad
  • 2,526
  • 10
  • 36
  • 73
  • how does one know that the id is 'grdrelatedcontacts'? ie where do i get the id for my grid from???? –  Oct 12 '11 at 09:40
  • grdrelatedContact is the grid name which you can find when you are on the form customization , just click on the grid and you will find the name of the grid – Asim Sajjad Oct 30 '11 at 14:05
0

Use a Rest call and retrieve the corresponding records :S

Mauro De Biasio
  • 1,146
  • 6
  • 16
0

You can do something like this:

var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/pws_streedandhousenodatas?$filter=_pws_streetandhousenumberid_value eq " + Xrm.Page.data.entity.getId(), true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
req.onreadystatechange = function() {
    if (this.readyState === 4) {
        req.onreadystatechange = null;
        if (this.status === 200) {
            var results = JSON.parse(this.response);
            for (var i = 0; i < results.value.length; i++) {
                var pws_streedandhousenodataid = results.value[i]["pws_streedandhousenodataid"];
            }
        } else {
            Xrm.Utility.alertDialog(this.statusText);
        }
    }
};
req.send();

In this case the Xrm.Page.data.entity.getId() get you your current record id and you are looking all the lookups (that are in the sub-grid), you can also add some fields to select more info from them.

Moran Barzilay
  • 133
  • 2
  • 13
0

You can inspect the subgrid values on save by doing the following:

var gridControl = document.getElementById('subgrid_id').control;
var ids = gridControl.get_allRecordIds();
for(i = 0; i < ids.length; i++) {
    var cellValue = gridControl.getCellValue('column_name', ids[i]);
    // logic
}

Doing this on load is a bit more challenging since subgrids are loaded asynchronously and aren't likely to be done loading when the form onload event fires. You can check the grid periodically though to see when it's done loading by calling a function like the following in your form onload:

function subGridOnload() {
    var grid = document.getElementById('subgrid_id');
    if (grid.readyState!="complete") {
        // delay one second and try again.  
        setTimeout(subGridOnload, 1000);
        return;
    }

    // logic
}
Dan Rigby
  • 17,133
  • 6
  • 43
  • 60