Retrieve rows in crm2011 subgrid with JScript

老子叫甜甜 提交于 2019-11-29 13:04:07

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);

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
}

Use a Rest call and retrieve the corresponding records :S

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!