locked
Checkboxes in CRM 2011 RRS feed

  • Question

  • Hi

    Newbie here and any guidance would be greatly appreciated

    I have 7 checkboxes (code below only shows 3 for simplicity

    Condition: I can only have 1 Checkbox selected at all times from the set of 7
    and it is required to only have one checkbox selected on the OnSave event

    The code below only looks at the onChange or onClick.
    In a new case , when I check the 1st checkbox the code goes through as expected
    with TRUE , FALSE , FALSE 

    However, when I select the 2nd checkbox the alerts display TRUE , TRUE, FALSE but 
    the 1st checkbox is not unchecked as the code is skipped

    any suggestions and thanks

    function CheckBox()  {
    var  myCheckboxValue1 = Xrm.Page.getAttribute("new_checkbox1a").getValue();
    var  myCheckboxValue2 = Xrm.Page.getAttribute("new_checkbox2a").getValue();
    var  myCheckboxValue3 = Xrm.Page.getAttribute("new_checkbox3a").getValue();

    alert(myCheckboxValue1);
    alert(myCheckboxValue2);
    alert(myCheckboxValue3);

    if (myCheckboxValue1 == true) {

    var  myCheckboxValue2 = Xrm.Page.getAttribute("new_checkbox2a").setValue(false);
    var  myCheckboxValue3 = Xrm.Page.getAttribute("new_checkbox3a").setValue(false);

    } else if  (myCheckboxValue2 == true) {
    var  myCheckboxValue2 = Xrm.Page.getAttribute("new_checkbox1a").setValue(false);
    var  myCheckboxValue3 = Xrm.Page.getAttribute("new_checkbox3a").setValue(false);

    }
    }


    Nick M

    Monday, August 31, 2015 4:07 PM

Answers

  • What is the logic to determine which checkbox should be selected OnSave? If a user selects say three checkboxes how does your OnSave code know which ones to clear?

    Although it sounds tedious, I would have an OnChange function for each checkbox field (that is seven functions) and the code for each one would be something like:  Is the checkbox selected?  If yes, then unselect all the other checkboxes (literally write out 6 statements like Xrm.Page.getAttribute("new_checkbox3a").setValue(false) for the checkboxes to be unselected.   You might be able to consolidate this to one function if you look at passing variables to the function.

    You don't need anything in OnSave.

    Thursday, September 3, 2015 9:13 AM
    Moderator

All replies

  • What is the logic to determine which checkbox should be selected OnSave? If a user selects say three checkboxes how does your OnSave code know which ones to clear?

    Although it sounds tedious, I would have an OnChange function for each checkbox field (that is seven functions) and the code for each one would be something like:  Is the checkbox selected?  If yes, then unselect all the other checkboxes (literally write out 6 statements like Xrm.Page.getAttribute("new_checkbox3a").setValue(false) for the checkboxes to be unselected.   You might be able to consolidate this to one function if you look at passing variables to the function.

    You don't need anything in OnSave.

    Thursday, September 3, 2015 9:13 AM
    Moderator
  • Thanks for your reply.

    I did get this to work late yesterday and I did something similar as what you were proposing.

    The code makes sure that only one checkbox is selected at any one time and the onsave makes sure that one is selected when saving

    Code for the  onchange event is called on each checkbox
    Passing and storing the value of selected Checkbox that is set to true.

    CODE:

    var selectedCheckbox = "default"

    function CheckBox()  {

    var  myCheckboxValue1 = Xrm.Page.getAttribute("new_checkbox1a").getValue();
    var  myCheckboxValue2 = Xrm.Page.getAttribute("new_checkbox2a").getValue();
    var  myCheckboxValue3 = Xrm.Page.getAttribute("new_checkbox3a").getValue();
    var  myCheckboxValue4 = Xrm.Page.getAttribute("new_checkbox4a").getValue();
    var  myCheckboxValue5 = Xrm.Page.getAttribute("new_checkbox5a").getValue();
    var  myCheckboxValue6 = Xrm.Page.getAttribute("new_checkbox6a").getValue();
    var  myCheckboxValue7 = Xrm.Page.getAttribute("new_checkbox7a").getValue();

    if (myCheckboxValue1 == true && selectedCheckbox != "new_checkbox1a")    {
    Xrm.Page.getAttribute("new_checkbox2a").setValue(false);
    Xrm.Page.getAttribute("new_checkbox3a").setValue(false);
    Xrm.Page.getAttribute("new_checkbox4a").setValue(false);
    Xrm.Page.getAttribute("new_checkbox5a").setValue(false);
    Xrm.Page.getAttribute("new_checkbox6a").setValue(false);
    Xrm.Page.getAttribute("new_checkbox7a").setValue(false);
    selectedCheckbox = "new_checkbox1a"

    } else if (myCheckboxValue2 == true && selectedCheckbox != "new_checkbox2a")   {
    Xrm.Page.getAttribute("new_checkbox1a").setValue(false);
    Xrm.Page.getAttribute("new_checkbox3a").setValue(false);
    Xrm.Page.getAttribute("new_checkbox4a").setValue(false);
    Xrm.Page.getAttribute("new_checkbox5a").setValue(false);
    Xrm.Page.getAttribute("new_checkbox6a").setValue(false);
    Xrm.Page.getAttribute("new_checkbox7a").setValue(false);
    selectedCheckbox = "new_checkbox2a"

    } else if (myCheckboxValue3 == true && selectedCheckbox != "new_checkbox3a")  {
    Xrm.Page.getAttribute("new_checkbox1a").setValue(false);
    Xrm.Page.getAttribute("new_checkbox2a").setValue(false);
    Xrm.Page.getAttribute("new_checkbox4a").setValue(false);
    Xrm.Page.getAttribute("new_checkbox5a").setValue(false);
    Xrm.Page.getAttribute("new_checkbox6a").setValue(false);
    Xrm.Page.getAttribute("new_checkbox7a").setValue(false);
    selectedCheckbox = "new_checkbox3a"

    }  else if (myCheckboxValue4== true && selectedCheckbox != "new_checkbox4a")  {
    Xrm.Page.getAttribute("new_checkbox1a").setValue(false);
    Xrm.Page.getAttribute("new_checkbox2a").setValue(false);
    Xrm.Page.getAttribute("new_checkbox3a").setValue(false);
    Xrm.Page.getAttribute("new_checkbox5a").setValue(false);
    Xrm.Page.getAttribute("new_checkbox6a").setValue(false);
    Xrm.Page.getAttribute("new_checkbox7a").setValue(false);
    selectedCheckbox = "new_checkbox4a"

    }  else if (myCheckboxValue5== true && selectedCheckbox != "new_checkbox5a")  {
    Xrm.Page.getAttribute("new_checkbox1a").setValue(false);
    Xrm.Page.getAttribute("new_checkbox2a").setValue(false);
    Xrm.Page.getAttribute("new_checkbox3a").setValue(false);
    Xrm.Page.getAttribute("new_checkbox4a").setValue(false);
    Xrm.Page.getAttribute("new_checkbox6a").setValue(false);
    Xrm.Page.getAttribute("new_checkbox7a").setValue(false);
    selectedCheckbox = "new_checkbox5a"

    } else if (myCheckboxValue6== true && selectedCheckbox != "new_checkbox6a")  {
    Xrm.Page.getAttribute("new_checkbox1a").setValue(false);
    Xrm.Page.getAttribute("new_checkbox2a").setValue(false);
    Xrm.Page.getAttribute("new_checkbox3a").setValue(false);
    Xrm.Page.getAttribute("new_checkbox4a").setValue(false);
    Xrm.Page.getAttribute("new_checkbox5a").setValue(false);
    Xrm.Page.getAttribute("new_checkbox7a").setValue(false);
    selectedCheckbox = "new_checkbox6a"

    } else if (myCheckboxValue7== true && selectedCheckbox != "new_checkbox7a")  {
    Xrm.Page.getAttribute("new_checkbox1a").setValue(false);
    Xrm.Page.getAttribute("new_checkbox2a").setValue(false);
    Xrm.Page.getAttribute("new_checkbox3a").setValue(false);
    Xrm.Page.getAttribute("new_checkbox4a").setValue(false);
    Xrm.Page.getAttribute("new_checkbox5a").setValue(false);
    Xrm.Page.getAttribute("new_checkbox6a").setValue(false);
    selectedCheckbox = "new_checkbox7a"
    }

    myCheckboxValue1 = Xrm.Page.getAttribute("new_checkbox1a").getValue();
    myCheckboxValue2 = Xrm.Page.getAttribute("new_checkbox2a").getValue();
    myCheckboxValue3 = Xrm.Page.getAttribute("new_checkbox3a").getValue();
    myCheckboxValue4 = Xrm.Page.getAttribute("new_checkbox4a").getValue();
    myCheckboxValue5 = Xrm.Page.getAttribute("new_checkbox5a").getValue();
    myCheckboxValue6 = Xrm.Page.getAttribute("new_checkbox6a").getValue();
    myCheckboxValue7 = Xrm.Page.getAttribute("new_checkbox7a").getValue();

    }

    Code for onsave and selected to PASS execution context as first parameter

    CODE:

    function ForceRequirement(executionObj)
    {
       var canSave = false;
       var fields = ["new_checkbox1a", "new_checkbox2a", "new_checkbox3a","new_checkbox4a", "new_checkbox5a", "new_checkbox6a", "new_checkbox7a"];

       for (index = 0; index < fields.length; index++)  {
          var checkboxValue = Xrm.Page.getAttribute(fields[index]).getValue();
          if (checkboxValue == true) {
                   canSave = true;
                   break;
           }
       }

    // stop the save event
       if (canSave == false)     {
            alert("You must select one option in the section.");
            executionObj.getEventArgs().preventDefault();
        }
    }


    Nick M

    Thursday, September 3, 2015 1:23 PM
  • Great, glad you got there.
    Thursday, September 3, 2015 1:37 PM
    Moderator
  • posted code to help others in future

    Thanks for your response. It was  greatly appreciated.

    Have a nice day


    Nick M

    Thursday, September 3, 2015 1:39 PM