HR Leave · Australia

Script — BR3_audit_state_change.js

ServiceNow Australia · Custom app · Arabic + English readable · scripts copy/paste ready

App typeCustom
Scopex_786686_hr_leave
sys_idc84db0e8839f0b509f02b0d6feaad3c9
Replace{BASE} = your instance
Instance URLs Application Leave requests Business Rules Studio

Copy this script into ServiceNow under application x_786686_hr_leave (Australia). Table/API names already use the current scope.

Source: src/server/business_rules/BR3_audit_state_change.js

/**
 * Business Rule: BR3 - Audit State Change + Balance
 * Application: Custom · Scope: x_786686_hr_leave · sys_id: c84db0e8839f0b509f02b0d6feaad3c9
 * Table:  x_786686_hr_leave_req_v2
 * When:   after
 * Update: true
 * Order:  300
 *
 * Writes an audit row on any state change and recalculates balance on approval.
 */
(function executeRule(current, previous) {

    if (!current.u_state.changes()) return;

    var util = new x_786686_hr_leave.HRLXProLeaveUtilsV2();
    util.auditStateChange(
        current.getUniqueValue(),
        previous ? previous.getValue('u_state') : '',
        current.getValue('u_state'),
        'state_change'
    );

    if (current.u_state.changesTo('approved') && current.u_employee_ref) {
        var year = new GlideDateTime(current.getValue('u_start_date')).getYearLocalTime();
        util.recalculateBalance(current.getValue('u_employee_ref'), year);
    }

})(current, previous);