HR Leave · Australia

06 — Notifications, Events & Jobs (Studio From Scratch)

3 events, 3 email notifications, scheduled accrual job with full script

Scopex_786686_hr_leave
sys_ide5f5bc9493a303507520703efaba104d
Instance labdev398980
URL token{BASE}
Quick open Visual map Events Notifications Scheduled Jobs

Visual map — Ch.06 · Events · mail · jobs

See in 10 seconds

3 events → 3 emails · annual accrual job

You build

leave_v2.submitted / approved / rejected

Track

Administration

REGRegister
→
BReventQueue
→
MAILTemplate
→
JOBAccrual 1 Jan
✓ Register before fire✓ Idempotent job✓ Test real SMTP✗ Email on every update✗ Job without skip-existing

Full visual path (all chapters): VISUAL_FROM_SCRATCH_00_16.html

Studio-only from scratch: Events are queued by BR2 and BR4 from 01 Development. Notifications and accrual job complete the async layer.

Also open: Properties + Direct Links · Schema · Studio Create All Objects · Previous: 05 Workspace

How events, notifications, and jobs relate

BR2 submit → Part 1 Event leave_v2.submitted → Part 2 Notification to approver
BR4 approve/reject → events approved/rejected → Part 2 Notifications to requester
Part 3 Scheduled Job → creates bal_v2 rows from property annual_days

Rule 0 — Before any object (Application + Update Set + Studio)

Repeat these checks before creating or editing any application file. This section is also section 00 Update Set — create the capture set first.

  1. Application picker — top-right globe / application menu → select HR Leave (x_786686_hr_leave). Never leave Global selected when creating scoped artifacts.
  2. Update Set — All → System Update Sets → Local Update Sets → New:
    FieldValue
    NameHR Leave – Build
    ApplicationHR Leave
    StateIn Progress
    DescriptionCaptures all HR Leave lab artifacts
    After submit: open the record → click Make Current. Direct list: {BASE}/sys_update_set_list.do
  3. Open Studio — {BASE}/$studio.do → confirm header shows HR Leave.

If artifacts land in Global scope, delete them and recreate under HR Leave. ACLs and events created globally will not behave correctly for this app.

Verify Update Set: Create a test Script Include in Studio → open HR Leave – Build → Related List Customer Updates shows the new file.


Part 1: Event Registry

Purpose

Registers three scoped events queued by BR2/BR4 for notifications and monitoring.

Navigation path

Direct URL

Prerequisites

Form fields

Event nameTableActive
x_786686_hr_leave.leave_v2.submittedx_786686_hr_leave_req_v2true
x_786686_hr_leave.leave_v2.approvedx_786686_hr_leave_req_v2true
x_786686_hr_leave.leave_v2.rejectedx_786686_hr_leave_req_v2true

Step-by-step create

  1. Registry → New (three times).
  2. Event name exactly as table. Table = Leave Request. Application = HR Leave. Active.

Verify checklist

Demo / test

Event parm1 = approver sys_id, parm2 = u_number.


Part 2: Notifications

Purpose

Three email notifications: submitted (to approver), approved/rejected (to requester).

Navigation path

Direct URL

Prerequisites

Form fields

Notification 1 — Submitted

FieldValue
NameHRLX Leave Submitted
Tablex_786686_hr_leave_req_v2
Activetrue
When to sendEvent is fired
Event namex_786686_hr_leave.leave_v2.submitted
Users/Groups in fieldsu_approver_user
SubjectLeave ${u_number} needs your approval

Notification 2 — Approved

Eventleave_v2.approved
Users/Groups in fieldsu_requested_for_user
SubjectYour leave ${u_number} was approved

Notification 3 — Rejected

Eventleave_v2.rejected
Users/Groups in fieldsu_requested_for_user
SubjectYour leave ${u_number} was rejected

Step-by-step create

  1. Application = HR Leave. Notifications → New for each.
  2. Tab When to send: Event is fired + event name.
  3. Tab Who will receive: Users/Groups in fields → pick u_approver_user or u_requested_for_user.
  4. Tab What it will contain: Subject + Message HTML with ${u_number}, ${u_start_date}, ${URI_REF}.
  5. Submit each notification. Active = true.

Verify checklist

Demo / test

Submit as employee1 → manager1 receives email or sys_email row.


Part 3: Scheduled Job — Annual Accrual

Purpose

Creates missing bal_v2 rows for current year for every employee using property annual_days.

Navigation path

Direct URL

Prerequisites

Form fields

FieldValue
NameHRLX Annual Accrual
ApplicationHR Leave
ActiveChecked
RunDaily 02:00 or Yearly Jan 1
Run asSystem (default)

Step-by-step create

  1. Scheduled Script Execution → New.
  2. Fill fields. Paste script below.
  3. Save. Click Execute Now to test.

Full script (paste-ready)

/**
 * Application: Custom · Scope: x_786686_hr_leave · sys_id: 83d5298c932b8f107520703efaba1072
 * 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.');
})();

Verify checklist

Demo / test

New employee → run job → u_entitled=20, u_remaining=20.

Notifications & Jobs verify checklist

Complete after Parts 1–3.

  1. Three events registered on req_v2.
  2. Three email notifications active.
  3. Accrual job creates balance rows without duplicates.