Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Dynamically count the number of Checkboxes marked "YES" contained within a Section on a Form

(0) ShareShare
ReportReport
Posted on by

Trying to figure out the best way that I can capture the number of checkboxes marked "Yes" that are all contained in one section on the contacts form.  I have several checkboxes that represent whether or not the contact belongs on a specific marketing list.  These contacts are always changing, and need to know how many lists each contact belongs to?  I'm a newbie when it comes down to JavaScript, if someone could point me in the right direction that would be awesome.

Thanks

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Dynamically count the number of Checkboxes marked "YES" contained within a Section on a Form

    I am trying to create this same type of workflow now.  Is there a limit to the number of times you can update a record within a workflow?  I currently have 11 checkboxes I need to total and I'm no longer able to select "Update Record,"  (I have one update for clearing the field and then 4 updates for increments ).  

  • maustin Profile Picture
    maustin on at
    RE: Dynamically count the number of Checkboxes marked "YES" contained within a Section on a Form

    This was the approach that I took, Like you said JavaScript can be hard to maintain.  It was a little tedious, with having 20+ checkboxes, but it works

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Dynamically count the number of Checkboxes marked "YES" contained within a Section on a Form

    yes finally my requirement made by using

    function checkbox() {

       var totalCount = 0;

       var yesCount = 0;

       Xrm.Page.data.entity.attributes.forEach(function (attr, index) {

           if (attr.getAttributeType() === "boolean") {

               totalCount++;

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

                   yesCount++;

               }

           }

       });

       alert("Total boolean field count: " + totalCount);

       alert("Total 'Yes' boolean field count: " + yesCount);

       if (totalCount == yesCount) {

           Xrm.Page.getAttribute("new_award").setValue("yes");

       }

       else {

           Xrm.Page.getAttribute("new_award").setValue(null);

       }

    }

  • RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Dynamically count the number of Checkboxes marked "YES" contained within a Section on a Form

    If I understood this correctly, you are looking for total field count and total 'yes' count.

    If this is so then you can try this -

    ------

    var totalCount = 0;

    var yesCount = 0;

      Xrm.Page.data.entity.attributes.forEach(function (attr, index) {

          if (attr.getAttributeType() === "boolean")

          {

              totalCount++;

              if (attr.getText() === "Yes")

              {

                  yesCount++;

              }

          }

      });

    alert("Total boolean field count: "+ totalCount);

    alert("Total 'Yes' boolean field count: "+ yesCount);

  • Suggested answer
    Nithya Gopinath Profile Picture
    Nithya Gopinath 17,076 on at
    RE: Dynamically count the number of Checkboxes marked "YES" contained within a Section on a Form

    Hi,

    You could also try the code below to get the controls in a section. Register this function on load as well as on change of the fields in the section.

    function countOfTrueCheckBoxes() {
        var count = 0;
        var section = Xrm.Page.ui.tabs.get("tabName").sections.get("sectionName");
        var controls = section.getControls();
        for (var i = 0; i < controls.getLength() ; i++) {
            if (controls.getByIndex(i).getAttribute().getValue() == true)
                count++;
        }
        Xrm.Page.getAttribute("countFieldName").setValue(count);
    }

    Hope this helps.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Dynamically count the number of Checkboxes marked "YES" contained within a Section on a Form

    hi ravi it worked but i need some other logic that is i want to compare no of option fields and no of fields that are Yes if both r equal then i will do some update pls let me know how to compare these

  • RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Dynamically count the number of Checkboxes marked "YES" contained within a Section on a Form

    try this-    

       var count = 0;

       Xrm.Page.data.entity.attributes.forEach(function (attr, index) {

           if (attr.getAttributeType() === "boolean")

           {

               if (attr.getText() === "Yes")

               {

                   count++;

               }

           }

       });

       alert(count);

  • Verified answer
    Ryan Maclean Profile Picture
    Ryan Maclean 3,070 on at
    RE: Dynamically count the number of Checkboxes marked "YES" contained within a Section on a Form

    How many checkboxes do you have?  How likely are they to change in the future?  (i.e. will you be likely to add more marketing lists and therefore more checkboxes to your contact form?)

    My personal approach is to shy away from Javascript if possible, because it's a headache to maintain as the product moves on.  I think you could accomplish this via a workflow. 

    Add a field to your entity called Marketing List Count, with the type Whole Number.

    Create a workflow that starts on Record Creation or On Change of any of the checkbox fields.  

    Add a first step that clears the the Marketing List Count, then add a Check Condition step for each of your checkboxes.  If the Checkbox equals Yes, then increment the Marketing List Count field by 1.  Repeat for each Checkbox.  

  • Suggested answer
    Luke Sartain Profile Picture
    Luke Sartain 1,266 on at
    RE: Dynamically count the number of Checkboxes marked "YES" contained within a Section on a Form

    It is possible to retrieve all controls within a section.

    https://msdn.microsoft.com/en-gb/library/gg328489.aspx

    Then you can loop through each control to see if it's an optionset.  

    var collection = Xrm.Page.ui.tabs.get(tabname).sections.get(sectioname).getControls();

    var count = 0;

    collection.forEach(arg, index)

    {

    var controlType = Xrm.Page.getControl(arg).getControlType();
    if(controlType == "optionset")
    {
    if(Xrm.Page.getAttribute(arg).getValue() == true)
    {
    count++;
    }
    }

    }

    Not had time to confirm the syntax of the above but it's definitely possible without having to get the value of each individual field.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Dynamically count the number of Checkboxes marked "YES" contained within a Section on a Form

    if more fields having means we have to write every field value so more lines of code cant we get all option set fields?

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,489 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,305 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans