HR Leave · Australia

Script — annual_accrual.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/scheduled_jobs/annual_accrual.js

/**
 * Application: Custom · Scope: x_786686_hr_leave · sys_id: c84db0e8839f0b509f02b0d6feaad3c9
 * Scheduled Job: Annual Leave Accrual
 * Run: yearly (e.g. Jan 1)
 *
 * Creates a fresh balance row per employee for the new year if missing.
 * Default entitlement is read from a property (fallback 21 annual days).
 */
(function annualAccrual() {

    var year = new GlideDateTime().getYearLocalTime();
    var annualEntitlement = parseInt(gs.getProperty('x_786686_hr_leave.config.annual_days', '21'), 10);

    var emp = new GlideRecord('x_786686_hr_leave_employee_v2');
    emp.query();

    var created = 0;
    while (emp.next()) {
        // skip if a balance for this year/type already exists
        var existing = new GlideRecord('x_786686_hr_leave_bal_v2');
        existing.addQuery('u_employee_ref', emp.getUniqueValue());
        existing.addQuery('u_year', year);
        existing.addQuery('u_leave_type', 'annual');
        existing.query();
        if (existing.hasNext()) continue;

        var bal = new GlideRecord('x_786686_hr_leave_bal_v2');
        bal.initialize();
        bal.u_employee_ref = emp.getUniqueValue();
        bal.u_year = year;
        bal.u_leave_type = 'annual';
        bal.u_entitled = annualEntitlement;
        bal.u_used = 0;
        bal.u_remaining = annualEntitlement;
        bal.insert();
        created++;
    }

    gs.info('[HRLX] Annual accrual ' + year + ' created ' + created + ' balance rows.');
})();