Skip to content Skip to sidebar Skip to footer

Making A Section On Crm 2011 Form Required

I want to make a 'section' on the form required. Basically the sections has checkboxes. The user is supposed to check atleast one. How can this be done?

Solution 1:

Inside your OnSave event you need to check if at least one checkbox is checked, if all are not selected you can stop the save event.

To stop the save event you need to pass the context to your onsave event:

Pass execution context

The function will looks like:

function onSave(executionObj)
{
// stop the save event
executionObj.getEventArgs().preventDefault();
}

to check the values you have several ways, the simplest one is to keep an array of them:

functiononSave(executionObj)
{
   var canSave = false;
   var fields = ["new_checkbox1", "new_checkbox2", "new_checkbox3"];
   for (index = 0; index < fields.length; index++)
   {
      var checkboxValue = Xrm.Page.getAttribute(fields[index]).getValue();
      if (checkboxValue == true)
      {
         canSave = true;
         break;
      }
   }
   if (canSave == false)
   {
      alert("At least one checkbox must be selected!");
      executionObj.getEventArgs().preventDefault();
   }
}

Solution 2:

Guido's answer is entirely correct, but I wanted to offer a different solution. Instead of performing the validation during the onSave Event, you can wire up OnChangeEvents for your checkboxes, that perform the same basic logic as Guido's except marking them as not required or required:

Xrm.Page.getAttribute(controlName).setRequiredLevel("required");
Xrm.Page.getAttribute(controlName).setRequiredLevel("none");

So for example, lets assume you have CheckBoxes 1, 2, & 3, and by default, they are marked as required.

When a user checks a checkbox, mark it as required, and mark the other checkboxes that are not checked as not required. If the user unchecks all, mark them all as required.

Doing this is harder in code, but provides a better user experience since they can see what is required before they update the form, and you don't have to worry about changing the OnSave default behavior.

Post a Comment for "Making A Section On Crm 2011 Form Required"