Contact Us Today!

Enforce a Section Requirement on a Microsoft Dynamics CRM 2011 Form

Enforce a Section Requirement on a Microsoft Dynamics CRM 2011 Form

Enforce a section requirement on a CRM 2011 form

Often times I've been tasked with creating a "Services/Products Interested In" section on a form containing a group of various checkbox fields so that a Sales Rep can indicate what services and products a Lead, Prospect or Customer is interested in. Sometimes the customer may want to take it a step further by disallowing a Sales Rep to save the record without marking at least one checkbox within the section. You could potentially add some javascript on the OnSave event and use Xrm.Page.getAttribute("theattributename").getValue() for each attribute to determine if all are false or null and then if they are, alert the user. However, if fields are ever removed or added within the section, you would need to remember to adjust the javascript accordingly and sometimes the customer may add new fields themselves and not be aware of the javascript changes needed. To avoid situations like these, I developed this function that will dynamically adjust if a field is ever removed or added within the section. The function loops through each control determining if it is a boolean attribute.
Note: If your section contained different attribute types other than boolean, you would change the line control.getAttribute().getAttributeType() == "boolean" to the appropriate type.
The function then gets a count of each boolean attribute within the section and compares it with how many of the boolean attributes have a null or false value. If the counts are the same, then the section has not been filled out. The user is alerted and the record does not save.

function forceRequirement(ExecutionObj) {
//identify the section and entity controls
var section = Xrm.Page.ui.tabs.get(0).sections.get(1);
var controls = Xrm.Page.ui.controls.get();
//variable to hold the number of boolean attributes where value is null or false
var NULLnumber = 0;
//variable to hold the number of boolean attributes within the section
var ATTnumber = 0;
//loop through all controls within the section
for (var i in controls) {
var control = controls[i];
var controlSection = control.getParent();
if (controlSection == section) {
//get a sum of the # of boolean attributes within the section
if (control.getAttribute().getAttributeType() == "boolean") {
ATTnumber = ATTnumber + 1;
if (control.getAttribute().getValue() == false || control.getAttribute().getValue() == null) {
<span"> NULLnumber = NULLnumber + 1;
}
}
}
}
//if all boolean attributes within the section are null or false, alert the user and prevent the onsave event from executing
if (NULLnumber == ATTnumber) {
alert("You must select at least one option from the Services/Products Interested In section.");
//Cancel onSave event
ExecutionObj.getEventArgs().preventDefault();
}
}

In order to use this function, it should be placed on the entity's OnSave event and "pass execution content as first parameter" should be marked within the handler property window.

In this example, you can see how it would look to the end user. On the Lead form, a Services/Products Interested In section contains various checkboxes that the Sales Rep must use to identify what the Lead is interested in. As you can see below, the Sales Rep attempted to Save the Lead without filling out the section and so they were alerted with a Warning and the record did not Save:

CRMBlog

For further info please contact us at info@beringer.net

Microsoft Dynamics CRM Product Page: www.beringer.net/solutions/microsoft-dynamics-crm


theProfessor

theProfessor

Rob is the CTO of Beringer Technology Group, and focuses his efforts on software development, cloud engineering, team mentoring and strategic technical direction. Rob has worked with Beringer since 2005, and has influenced every department from Development, Security, Implementation, Support and Sales. Rob graduated with his MBA from Rowan University in 2012, earned his Bachelors of Computer Science in 1997, and is current with several Microsoft technical certifications. Rob is very active, and loves to mountain bike, weight train, cook and hike with his dog pack.