Search code examples
javascriptdynamics-crmdynamics-crm-365

Can't get setValue to work on a Dynamics CRM editable grid


I was hoping to get some pointers on how to do a setValue in a Dynamics CRM 365 (onpremise) editable grid:

No matter what I try I cannot update the values in the grid. This code gets a reference of the attribute but setValue seems to have no effect on the grid.

    function updateDocsOK(ctlName, grdName, attributeName) {
    var selectedRow = null;
    var attributeColl = null;
    var twoOptionValue = 0;


    try {

        //This is the Yes/No value in the dropdown
        var ctlValue = Xrm.Page.getAttribute(ctlName).getValue();
        if (ctlValue) {
            twoOptionValue = 1;
        }
        //get the selected rows - use the getControl method and pass the grid name.
        selectedRow = Xrm.Page.getControl(grdName).getGrid().getRows();

        //loop through rows and get the attribute collection
        selectedRow.forEach(function (row, rowIndex) {


            var att = row.getData().getEntity().attributes.getByName(attributeName);

            //This setValue does not work on a two-option
            if (att) {
                console.log(att.getValue());
                att.setValue(twoOptionValue);
                console.log(att.getValue());
            }


            //This setValue does not work on a text field
            att = row.getData().getEntity().attributes.getByName("new_testtext");

            if (att) {
                att.setValue("hello");
            }


        });
    } catch (e) {
        Xrm.Utility.alertDialog(e.message);
    }
}

Solution

  • You should select the option to pass execution context, and use executionContext.getFormContext() to get the current row in editable grid.

    function updateDocsOK(executionContext) {
        var entityObject = executionContext.getFormContext().data.entity;
        var att = entityObject.attributes.getByName("new_testtext");
        att.setValue("hello");
     }
    

    you can’t use Xrm.Page commands on editable grids. In my example I’ll want to set the probability value.

    On the form something like Xrm.Page.getAttribute(“closeprobability”).setValue(80) would do the trick. But this won’t work on editable grids.

    Instead I need to use a new method that has been released with Dynamics 365 (getFormContext).

    getFormContext returns a reference for either the form (Xrm.Page) or the editable grid (GridRow). Meaning we can now have code that will work in both situations.

    Read more