0
votes

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");
    }
}
3

3 Answers

2
votes

Instead of

if (att.getText() = "Yes") {  

try this

if (att.getText() == "Yes") {

Single equal = is for assignment whereas you want double equal == which is for comparison.

1
votes

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");
    }
}
0
votes

You have to initialise idqualityyescount to declare it as an integer:

var idqualifyyescount = 0;