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 :
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

I have the same question (0)
  • Suggested answer
    Community Member Profile Picture
    on at

    I am making a wide assumption based on my answer below, but I'm guessing you just want to know which checkbox fields are checked. Here is a good start, you might need to make a few tweaks to the code:

    function checkedOptionSet() {
        var firstOptionSet = Xrm.Page.data.entity.attributes.get('fieldname').getText();
        var secondOptionSet = Xrm.Page.data.entity.attributes.get('fieldname').getText();
        var thirdOptionSet = Xrm.Page.data.entity.attributes.get('fieldname').getText();
        var optionsetValues = [firstOptionSet,secondOptionSet,thirdOptionSet];    
    
        var count = 0;
        var yesText = "Yes";
    
        for (var i = 0; i < optionsetValues.length; i++) {
            if (i == yesText)
                count++;
        }
    }
  • maustin Profile Picture
    on at

    Thank you for your quick response.  If I wanted to take the count of checkboxes checked and put that value into a field, how would I populate that field.

  • Suggested answer
    Nithya Gopinath Profile Picture
    17,078 on at

    Hi,

    For copying this count to another field, you could modify the above code as shown below.

    function checkedOptionSet() {
        var firstOptionSet = Xrm.Page.data.entity.attributes.get('fieldname').getText();
        var secondOptionSet = Xrm.Page.data.entity.attributes.get('fieldname').getText();
        var thirdOptionSet = Xrm.Page.data.entity.attributes.get('fieldname').getText();
        var optionsetValues = [firstOptionSet,secondOptionSet,thirdOptionSet];    
    
        var count = 0;
        var yesText = "Yes";
    
        for (var i = 0; i < optionsetValues.length; i++) {
            if (i == yesText)
                count++;
        }
         
        Xrm.Page.getAttribute('fieldname').setValue(count);
    }

    Hope this helps.

  • Community Member Profile Picture
    on at

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

  • Suggested answer
    Luke Sartain Profile Picture
    1,266 on at

    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.

  • Verified answer
    Ryan Maclean Profile Picture
    3,070 on at

    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.  

  • RaviKashyap Profile Picture
    55,410 Moderator on at

    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);

  • Community Member Profile Picture
    on at

    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

  • Suggested answer
    Nithya Gopinath Profile Picture
    17,078 on at

    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.

  • RaviKashyap Profile Picture
    55,410 Moderator on at

    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);

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 > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans