web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Need help finishing JavaScript event that counts the total "Yes"s in a grid and gives a warning message if the "Yes" count >4

(0) ShareShare
ReportReport
Posted on by

In my Opportunity form, I've added a Sales Quota Distribution Grid (See screenshot below).  If more than four Yes's have been entered in the entire grid, I want a warning message (or something similar) to be shown.  For example, in the screenshot below, the "svc, Azure" account has 4 fields marked "Yes".  The "XXXX, XXXX" account has zero.  If I change one the distributions next to "XXXX, XXXX", I want the warning message to appear.

8171.SalesQD.PNG

I'm trying to create a JavaScript event to get this functionality to work.  However, I don't have much JavaScript experience and could use some help getting this to work.  Here is what I have so far:

function GetTotalYesCount() {
    var selectedRow = null;
    var attributeColl = null;
    var idqualifyyescount;
    var sowbomyescount;
    var scopeyescount;
    var closeyescount;
    try {
        //get the selected rows - use the getControl method and pass the grid name.
        selectedRow = Xrm.Page.getControl("s_qd").getGrid().getSelectedRows();
        //loop through rows and get the attribute collection
        selectedRow.forEach(function (row, rowIndex) {
            //get the attribute Collection
            attributeColl = row.getData().getEntity().attributes;
                switch (att.getName()) {
                    case "new_idqualify":
                        if (att.getValue() = "Yes") {
                            idqualifyyescount = idqualifyyescount +1;
                    case "new_sowbom":
                        if (att.getValue() = "Yes") {
                            sowbomyescount = sowbomyescount +1;
                        }
                    case "new_scope":
                        if (att.getValue() = "Yes") {
                            scopeyescount = scopeyescount +1;
                        }
                    case "new_close":
                        if (att.getValue() = "Yes") {
                            closeyescount = closeyescount +1;
                        }
                        }
                        if ((idqualifyyescount + sowbomyescount + scopeyescount +closeyescount) > 4) {                         
                            Xrm.Utility.alertDialog("More than 4 Yes's have been entered in the Sales Quota Distribution.");
                        }                      
}
}
}
}
Am I on the right track?  Any help getting this to work would be greatly appreciated.  Thanks in advance!!

I have the same question (0)
  • Community Member Profile Picture
    on at
    RE: Need help finishing JavaScript event that counts the total "Yes"s in a grid and gives a warning message if the "Yes" count >4

    Okay, that makes sense.  Thank you for all your help Ajyendra, I appreciate it!

  • ajyendra Profile Picture
    1,738 on at
    RE: Need help finishing JavaScript event that counts the total "Yes"s in a grid and gives a warning message if the "Yes" count >4

    You have to move that subgrid to Tab 2 to first Tab(Summary).IF you don't have any problem to shift to tab 1 (Summary) . Then you have to modified some code from the existing code or I think no need but still you can check. Then no need for Variable.

  • Suggested answer
    ajyendra Profile Picture
    1,738 on at
    RE: Need help finishing JavaScript event that counts the total "Yes"s in a grid and gives a warning message if the "Yes" count >4

    Hi,

    It will not work for old record means for old record you don't have subgrid details for updating that new field "new_displayquotadistributionerror". Here is the workaround If you really need that thing onload . Just put that grid on Summary Tab Create a new section with one column and put that subgrid on that.

  • Community Member Profile Picture
    on at
    RE: Need help finishing JavaScript event that counts the total "Yes"s in a grid and gives a warning message if the "Yes" count >4

    Would it be possible to update an Opportunity entity text field on the form based on whether or not there are more than 4 "Yes's" in the Sales Quota Distribution field?  I created a text field called "new_displayquotadistributionerror" and added the following to my JavaScript event:

    Xrm.Page.getAttribute("new_displayquotadistributionerror").setValue("Yes");

    However, it did not update the field successfully.  I'm thinking maybe I could display the warning message based on whether or not the new text field is set to "Yes".  Would this be a good workaround?  Is there another workaround I could use to display the error message when the Opportunity form is opened rather than when the grid is loaded?

    Thanks!

  • Suggested answer
    ajyendra Profile Picture
    1,738 on at
    RE: Need help finishing JavaScript event that counts the total "Yes"s in a grid and gives a warning message if the "Yes" count >4

    Hi @tgiard,

    this event will call onLoad event of Opportunity form but it doesn't means it will work onChnage event . onLoad event of the Form , we cannot get the rows until subgrid not loaded .It look like it will work on onChange event but it work on Subgrid load.

    Check that This scenario;

    1) Open Opportunity Form.

    2) NAvigate to tab where the sales Quota Distribution subgrid is present.

    3) When you navigate to that tab where subgrid is present then also it will check for  if it is more than 4 Yes's that it will popup that alert message.

    Hope it helps.

  • Community Member Profile Picture
    on at
    RE: Need help finishing JavaScript event that counts the total "Yes"s in a grid and gives a warning message if the "Yes" count >4

    This worked!  Thank you!  Would it be possible to have this trigger as an OnLoad event when the Opportunity form is loaded rather than as an OnChange event when the Sales Quota Distribution fields are updated?  That is what I'm hoping to accomplish.

  • Verified answer
    ajyendra Profile Picture
    1,738 on at
    RE: Need help finishing JavaScript event that counts the total "Yes"s in a grid and gives a warning message if the "Yes" count >4

    Hi,

    I created the same scenario from my end where I used contact subgrid in account entity.

    Note: Please Change the field name as per yours

    It works for me

    function GetTotalYesCount(executionContext) {

    var execContext = executionContext;

    var formContext = executionContext.getFormContext();

    var idqualifyyescount = 0;

    var sowbomyescount= 0;

    var scopeyescount= 0;

    var closeyescount= 0;

    try {

    //get rows - use the getControl method and pass the grid name.

    //var gridContext = formContext.getControl("Contacts");

    if(formContext.getGrid().getTotalRecordCount() == 0){

    setTimeout(function () { GetTotalYesCount(execContext); }, 2000);

    return;

    }

    var allRows = formContext.getGrid().getRows();

    //loop through rows and get the attribute collection

    allRows.forEach(function (row, rowIndex) {

    //get the attribute Collection

    var attributeColl = row.getData().getEntity().attributes;

    attributeColl.forEach(function (att, attrIndex) {

    switch (att.getName()) {

    case "new_twooption":

    if (att.getValue() == true) { //att.getValue() == "Yes"

    idqualifyyescount = idqualifyyescount +1;

    }

    break;

    case "new_twooption1":

    if (att.getValue() == true) {

    sowbomyescount = sowbomyescount +1;

    }

    break;

    case "new_twooption2":

    if (att.getValue() == true) {

    scopeyescount = scopeyescount +1;

    }

    break;

    case "new_twooption3":

    if (att.getValue() == true) {

    closeyescount = closeyescount +1;

    }

    break;

    }

    });

    });

    if ((idqualifyyescount + sowbomyescount + scopeyescount +closeyescount) > 4) {

    Xrm.Utility.alertDialog("More than 4 Yes's have been entered in the Sales Quota Distribution.");

    }

    }catch(err){

    console.log('Error occurred :' + err)

    }

    }

    function testCase(executionContext){

    var formContext = executionContext.getFormContext();

    formContext.getControl("Contacts").addOnLoad(GetTotalYesCount);

    }

    that testCase function is called in Form Onload event of Account in my case(Check as per yours)

  • Community Member Profile Picture
    on at
    RE: Need help finishing JavaScript event that counts the total "Yes"s in a grid and gives a warning message if the "Yes" count >4

    I tried adding this as "OnChange" event as well, but it did not work.

  • Community Member Profile Picture
    on at
    RE: Need help finishing JavaScript event that counts the total "Yes"s in a grid and gives a warning message if the "Yes" count >4

    Thank you.  I updated the code but it is still not working.  There is no error, but the warning message does not appear despite entering more than 4 "Yes"'s in the editable grid.

    As for the four variables, I wanted to have one variable for each field, but I understand it may make more sense to only use one variable.

  • Suggested answer
    ajyendra Profile Picture
    1,738 on at
    RE: Need help finishing JavaScript event that counts the total "Yes"s in a grid and gives a warning message if the "Yes" count >4

    I don't understand why you take four variable in your code but still here is the updated code

    function GetTotalYesCount(executionContext) {

    var formContext = executionContext.getFormContext();


    var idqualifyyescount = 0;

    var sowbomyescount= 0;

    var scopeyescount= 0;

    var closeyescount= 0;

    try {

    //get rows - use the getControl method and pass the grid name.

    var gridContext = formContext.getControl("s_qd");

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

    //loop through rows and get the attribute collection

    allRows.forEach(function (row, rowIndex) {

    //get the attribute Collection

    var attributeColl = row.getData().getEntity().attributes;
    attributeColl.forEach(function (att, attrIndex) {
    switch (att.getName()) {

    case "new_idqualify":

    if (att.getValue() == "true") { //att.getValue() == "Yes"

    idqualifyyescount = idqualifyyescount +1;

    }

    case "new_sowbom":

    if (att.getValue() == "true") {

    sowbomyescount = sowbomyescount +1;

    }

    case "new_scope":

    if (att.getValue() == "true") {

    scopeyescount = scopeyescount +1;

    }

    case "new_close":

    if (att.getValue() == "true") {

    closeyescount = closeyescount +1;

    }

    }
    });

    });

    if ((idqualifyyescount + sowbomyescount + scopeyescount +closeyescount) > 4) {

    Xrm.Utility.alertDialog("More than 4 Yes's have been entered in the Sales Quota Distribution.");

    }

    }catch(err){
    console.log('Error occurred :' + err)
    }

    }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Daniyal Khaleel Profile Picture

Daniyal Khaleel 150

#2
DAnny3211 Profile Picture

DAnny3211 73

#3
Abhilash Warrier Profile Picture

Abhilash Warrier 66 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans