gymcode
gymcode

Reputation: 4633

How To Loop Rows (other than current row) in Subgrid using JS in Dynamics 365

I am able to read a Subgrid cell value triggered by OnChange using web resource.

Following which, I would need to loop through the remaining rows (except current row) in the subgrid and read each cell value for each row.

May I know how can it be done?


Current code snippet:

function SetNA(context) {

    debugger;

    //id of the subgrid
    var id = formContext.data.entity.getId();

    // get the attribute which fired the onchange.
    var changedFirstNameAttr = context.getEventSource();

    // get the container for the attribute.
    var attrParent = changedFirstNameAttr.getParent();

    // var FirstName Field Attribute
    var changedFirstNameField = attrParent.attributes.get("firstName");

    // get the value of the changed first name value
    var changedFirstNameValue = changedFirstNameAttr.getValue();

    alert(changedFirstNameValue);

    if (changedFirstNameValue != null)
    {
        //loop through other rows in the subgrid, and read each cell value
    }
}

Upvotes: 0

Views: 3292

Answers (2)

strattonn
strattonn

Reputation: 2012

The existing answer is correct but you can't await in a forEach. If you need to await an Xrm operation on each record, a standard for loop with a getByIndex works.

let rows = selectedControl.getGrid().getSelectedRows()
for (let indx = 0;indx < rows.getLength(); indx++) {
    let update = {new_fieldname:"new value"}
    let proof = rows.getByIndex(indx)
    await Xrm.WebApi.updateRecord("new_tablename", proof.data.entity.getId(), update)
            .then(res => {console.log(res);})
}

Upvotes: 1

You should be able to use getRows method to pull and iterate through all the editable grid rows and achieve what you want.

var gridRows = gridContext.getGrid().getRows();

//loop through each row to get values of each column
gridRows.forEach(function (row, i) {
    var gridColumns = row.getData().getEntity().getAttributes();
    //loop through each column in row
    gridColumns.forEach(function (column, j) {
        var atrName = column.getName();
        var atrValue = column.getValue();
        });
    });

Read more

Upvotes: 1

Related Questions