CRM forms can often have multiple fields marked as business required. Mandatory validation is sometimes even added dynamically via JavaScript, for example based off the value of an option set. If you have a large CRM form, it's not always possible to see all of the red (*) asterisks; users will have to try and save the form over and over to be prompted and receive focus of each missing field individually.

You can instead program all of your business required fields into an array in JavaScript, and present all of the missing information in one popup box. This is more user-friendly.

First, setup your array of fields as objects where each object holds the schema name, label and value of the field:

var requiredFields = [{ shema: 'firstname' }, { schema: 'lastname' }, { schema: 'gendercode' }];

requiredFields.forEach(function(f) {
    f.label = Xrm.Page.getControl(f.schema).getLabel();
    f.value = Xrm.Page.getAttribute(f.schema).getValue();
});

// requiredFields[0] = { schema: 'firstname', label: 'First Name', value: '' }

Then iterate through all of your fields to determine which, if any, don't contain a value (which they should because they're business required):

var nullFields = [];

requiredFields.forEach(function(f) {
    if (!f.value)
        nullFields.push(f.label);
});

Note: I've chosen to use Array.prototype.forEach() in this example because it's supported in IE9 and above. If your CRM user base is using a more modern browser, consider using let:

// Introduced in ECMAScript 2015
for (let f of requiredFields) {
    if (!f.value)
        nullFields.push(f.label);
}

More information on array iteration and JavaScript browser compatibility here.

Anyway, we finally need to format the labels of our fields which are missing information into a readable message to be presented as an alert. The following code splits the array of field labels each with a comma, except the last element where an 'and' is placed:

var message = 'Please enter information for the following required fields: ' + 
    [nullFields.slice(0, -1).join(', '), nullFields.slice(-1)[0]].join(nullFields.length < 2 ? '' : ' and ');

// nullFields.slice(0, -1).join(', '): takes all but the last element and joins them together with a comma
// nullFields.slice(-1)[0]: it's the last element
// .join(nullFields.length < 2 ? '' : ' and ') joins that string and the last element with and if there are at least two elements

You can then present your alert with alert(message);:

As an alternate message formatting, you could use the • unicode character and the \n escape character to mock bulletpoints. Unfortunately, html tags can't be passed to alert boxes, otherwise we'd use ul:


Note: the example above only uses three fields. The solution presented should probably only be used for a requirement involving 10+ fields. Adding JavaScript adds complexity to the system and makes it harder to maintain and should therefore be avoided where possible.