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

Notifications

Announcements

No record found.

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)
  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi ,

    You are almost correct way to do this , as you are saying that you are comparing "Yes" value in the entire grid so you should use "getrows" to get  the total no of "Yes" count first in the entire grid  then on selected row you can check all the value using for loop and put condition  if its exceeds more than 4 or not based on total record count.

    below method helps you to get total "Yes" count in the entire grid .

    docs.microsoft.com/.../getrows

    Second I would suggest to use formcotext instead of Xrm.Page.

  • Community Member Profile Picture
    on at

    Hi Goutan,

    Thank you for the help!  Please see my updated code below.  I updated the code based on your feedback, but it still does not work.  Please see code below:

    function GetTotalYesCount(executionContext) {

       var formContext = executionContext.getFormContext();

       var allRows = null;

       var attributeColl = null;

       var idqualifyyescount;

       var sowbomyescount;

       var scopeyescount;

       var closeyescount;

       try {

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

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

           //loop through rows and get the attribute collection

           allRows.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.");

                           }                      

    }

    }

    }

    }

    Does this look correct?  I apologize if I misunderstood what needed to be updated.

    Another question I have: does the event need to be added to the Opportunity form, or do I need to add it to the grid within the Opportunity form?

    Thanks!!

  • Community Member Profile Picture
    on at

    @Goutam,

    One other thing to note: I am no longer specifying that "s_qd" is the grid that needs to be referenced due to adding "allRows = gridContext.getGrid().getRows();".  Is there somewhere else in the code I should specify that the code should reference the "s_qd" grid?

    Thank you!

  • Suggested answer
    ajyendra Profile Picture
    1,738 on at

    You forgot to add this  var gridContext = formContext.getControl("s_qd"); line before allRows = gridContext.getGrid().getRows();

  • Community Member Profile Picture
    on at

    @Ajyendra Singh,

    Thank you!  I've re-added it to the code.  However, I am still receiving the error "Web resource method does not exist: GetTotalYesCount". How can I fix this?

  • ajyendra Profile Picture
    1,738 on at

    Can you share with me When you will call this method and HOw to configure it to call this method in events.

    Check the below link how to configure

    www.tutorialspoint.com/.../microsoft_crm_jscript_web_resources.htm

    If you have done any mistake pls cross check if not then Please Share the event from which you called that method (prefer screenshot)

  • ajyendra Profile Picture
    1,738 on at

    When you look on this link cross check this thing carefully

    pastedimage1573452951248v1.png

  • Community Member Profile Picture
    on at

    @Ajyendra,

    Thank you.  I believe I've configured this correctly.  Please see screenshot below.

    HandlerProperties.PNG

    The event is getting triggered "OnLoad" of the Opportunity form.  Any ideas?

  • Suggested answer
    ajyendra Profile Picture
    1,738 on at

    i think webresource not publish . Check that setting->Customization->select Webresource(from left navigation) -> select the webresoure(new_salesQuotaDistributionYesCount) and open and check this method is Exist there or not . if Exists then Publish it .

    Try again

  • Community Member Profile Picture
    on at

    I've confirmed the web resource exists and republished it.  Unfortunately, the error still occurs when I try again.

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 83 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 49 Most Valuable Professional

#3
#ManoVerse Profile Picture

#ManoVerse 40

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans