Require notes field to be populated on time entries when more than 8 hours in a day

This script validates that the notes field has been populated on time entries when more than 8 hours in a day.

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

Setup

  1. Create a new Timesheet [Edit] 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 approval event, and set require_timeentry_notes_daily_overtime as the Entrance Function.

    Scripting Studio tools and settings panel showing the form association, the user event association and the selected entrance function.

Program Listing

          function require_timeentry_notes_daily_overtime() {

    // Load task data
    var task = new NSOA.record.oaTask();
    task.timesheetid = NSOA.form.getOldRecord().id;
    NSOA.meta.log('debug', "got ts ID " + task.timesheetid);

    var readRequest = {
        type: "Task",
        fields: "id, date, decimal_hours, notes",
        method: "equal to",
        objects: [task],
        attributes: [{
            name: "limit",
            value: "1000"
        }]
    };

    var arrayOfreadResult = NSOA.wsapi.read(readRequest);

    // Analyze the timesheet
    var ts_total_by_day = {};
    var task_no_notes_by_day = {};
    if (!arrayOfreadResult || !arrayOfreadResult[0])
        NSOA.form.error('', "Internal error loading timesheet details.");

    else if (arrayOfreadResult[0].errors === null && arrayOfreadResult[0].objects)
        arrayOfreadResult[0].objects.forEach(
            function(o) {
                // Get yyyy-mm-dd part
                var date = o.date.substr(0, 10);

                // Track total hours for this day
                if (ts_total_by_day[date] === undefined)
                    ts_total_by_day[date] = Number(o.decimal_hours);
                else if (ts_total_by_day[date] <= 8)
                    ts_total_by_day[date] += Number(o.decimal_hours);
                else
                    return; // Already reported form error if we got here
                NSOA.meta.log('debug', date + " -> " + ts_total_by_day[date]);

                // Track time entries with no notes
                if (task_no_notes_by_day[date] === undefined)
                    task_no_notes_by_day[date] = [];
                if (o.notes === undefined || o.notes.length === 0)
                    task_no_notes_by_day[date].push(o.id);

                // Check the policy
                if (ts_total_by_day[date] > 8 && task_no_notes_by_day[date] !== undefined &&
                    task_no_notes_by_day[date].length > 0) {
                    NSOA.meta.log('trace', ts_total_by_day[date] + " -> " + task_no_notes_by_day[date].length);
                    NSOA.form.error('', "Notes are required on all " + date +
                        " time entries: total reported time that day is more than 8 hours.");
                }
            }
        );
}