Ensure value of multiple commissions fields equals 100%

This script checks to ensure that sales commission amounts equal 100% (1.00) before allowing the project to be saved. It can be modified to support any number of sales rep commissions fields.

A new custom Commission section has been added to the project form. A user script is triggered as the project saves to validate the commission values entered.

Commission custom section on the project properties form in OpenAir.

Follow the steps below or download the solutions file, see Creating Solutions for details.

Setup

  1. Create a new Project form script deployment.

  2. Enter a Filename and click SAVE. The extension ‘.js’ is automatically appended if not supplied.

  3. Click on the script link to launch the Scripting Studio.

  4. (1) Copy the Program Listing below into the editor, (2) set the Before save event, and set checkCommish as the Entrance Function.

    Scripting Studio tools and settings panel showing the form association, the user event association and the selected entrance function.
  5. Set up the required number of custom field pairs for Project. The first in each pair is a Pick List with a List source of User. The second in each pair is a Ratio. You can set the Divider text for the first custom field to Commission to place the custom fields in their own section.

    Custom field list view showing the commission custom fields.
  6. Use the Form schema to identify the correct param names for the custom fields and change the array at the top of the script accordingly.

    Scripting Studio form schema explorer listing the commission custom fields.
    Tip:

    If the new custom fields are not listed in the Form schema, navigate to Projects, open a project form (this with will refresh the custom field list), and then open the Scripting Studio again.

Program Listing

          // ADD YOUR REP AND RATIO CUSTOM FORM FIELD NAMES TO THE ARRAY BELOW
var repCompFlds = [
    'prj_sales_rep_1__c', 'prj_sales_rep_ratio_1__c', // Use Form schema to find param names
    'prj_sales_rep_2__c', 'prj_sales_rep_ratio_2__c'
];

function checkCommish(type) {

    try {
        var _len = repCompFlds.length,
            _i = 1, // Skip over sales rep name
            _j = 0,
            totalComp = 0;

        while (_i < _len) {
            totalComp += parseFloat(NSOA.form.getValue(repCompFlds[_i]));
            _i += 2; // Skip over sales rep name
        }

        var totalCompRound = round(totalComp, 2),
            totalCompPercent = totalCompRound * 100;
        if (totalCompRound !== 0 && totalCompRound != 1) {

            NSOA.form.error(
                '',
                'The total sales commission ' + totalCompRound +
                ' (' + round(totalCompPercent, 2) + '%) must equal 100%!'
            );

            for (_j; _j < _len; _j++) {
                NSOA.form.error(repCompFlds[_j], 'Please check and re-save.');
            }
        }

    } catch (e) {
        NSOA.meta.log('fatal', "Error running the script: " + e);
    }

}

function round(number, decimals) {
    return (Math.round(number * Math.pow(10, 2)) / Math.pow(10, 2)).toFixed(decimals);
}