Search code examples
javascriptdynamics-crm

JavaScript/Dynamics CRM: Need help finishing JavaScript event that counts how many records in a grid's "Id & Qualify" field ="Yes"


In my Opportunity form, I've added a "Sales Quota Distribution" entity editable grid. One Opportunity can have many Sales Quota Distribution records. I am working on a JavaScript event that counts how many records = "Yes" in the "ID & Qualify" field for records shown in the Sales Quota Distrubution grid. "ID & Qualify" is a dropdown field, and the two options it contains are Yes and No. I believe I am close to being done, but the code is returning "null" for some reason. Any help getting this to work would be greatly appreciated. Thank you!

Here is my code, which is currently returning null regardless of how many "Yes"'s are shown in the "Id & Qualify" field in the grid:

function YesCount(executionContext) {
    var formContext = executionContext.getFormContext();
    var allRows = null;
    var attributeColl = null;
    var idqualifyyescount;
    var gridContext = formContext.getControl("s_qd");
    allRows = gridContext.getGrid().getRows();
    allRows.forEach(function (row, rowIndex) {
        attributeColl = row.getData().getEntity().attributes;
        switch (att.getName()) {
            case "new_idqualify":
                if (att.getText() = "Yes") {
                    idqualifyyescount = idqualifyyescount + 1;
                }
        }
    });
    if ((idqualifyyescount) > 4) {
        Xrm.Page.ui.setFormNotification("WARNING: There are more than 4 Yes's in Sales Quota Distribution grid.", "WARNING");
    }
}

Solution

  • When are you setting att? Try this.

    function YesCount(executionContext) {
    var formContext = executionContext.getFormContext();
    var allRows = null;
    var attributeColl = null;
    var idqualifyyescount;
    var gridContext = formContext.getControl("s_qd");
    allRows = gridContext.getGrid().getRows();
    allRows.forEach(function (row, rowIndex) {
        attributeColl = row.getData().getEntity().attributes;
        attributeColl.forEach(function(att) {
            switch (att.getName()) {
                case "new_idqualify":
                    if (att.getText() == "Yes") {
                        idqualifyyescount = idqualifyyescount + 1;
                    }
            }
        });
    });
    if ((idqualifyyescount) > 4) {
        Xrm.Page.ui.setFormNotification("WARNING: There are more than 4 Yes's in Sales Quota Distribution grid.", "WARNING");
    }
    

    Or Better yet.

    function YesCount(executionContext) {
        var formContext = executionContext.getFormContext();
        var idqualifyyescount = 0;
        var gridCtx = formContext.getControl("s_qd");
    
        gridCtx.getGrid().getRows().forEach((row) => {
            let att = row.getData().entity.getAttributes().getByName("new_idqualify");
            if(att.getText() == "Yes")
                idqualifyyescount++;
        });
    
        if(idqualifyyescount > 4){
            Xrm.Page.ui.setFormNotification("WARNING: There are more than 4 Yes's in Sales Quota Distribution grid.", "WARNING");
        }
    }