Visual map — Ch.06 · Events · mail · jobs
3 events → 3 emails · annual accrual job
leave_v2.submitted / approved / rejected
Administration
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.
- Application picker — top-right globe / application menu → select HR Leave (
x_786686_hr_leave). Never leave Global selected when creating scoped artifacts. - Update Set — All → System Update Sets → Local Update Sets → New:
After submit: open the record → click Make Current. Direct list: {BASE}/sys_update_set_list.doField Value Name HR Leave – BuildApplication HR Leave State In Progress Description Captures all HR Leave lab artifacts - 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
- Part 3: Table req_v2 table.
Form fields
| Event name | Table | Active |
|---|---|---|
x_786686_hr_leave.leave_v2.submitted | x_786686_hr_leave_req_v2 | true |
x_786686_hr_leave.leave_v2.approved | x_786686_hr_leave_req_v2 | true |
x_786686_hr_leave.leave_v2.rejected | x_786686_hr_leave_req_v2 | true |
Step-by-step create
- Registry → New (three times).
- Event name exactly as table. Table = Leave Request. Application = HR Leave. Active.
Verify checklist
- Submit req → System Logs → Events shows leave_v2.submitted.
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
- Part 1: Events events.
- Part 9: Business Rule BR2 BR2, Part 11: Business Rule BR4 BR4.
- Users have email addresses.
Form fields
Notification 1 — Submitted
| Field | Value |
|---|---|
| Name | HRLX Leave Submitted |
| Table | x_786686_hr_leave_req_v2 |
| Active | true |
| When to send | Event is fired |
| Event name | x_786686_hr_leave.leave_v2.submitted |
| Users/Groups in fields | u_approver_user |
| Subject | Leave ${u_number} needs your approval |
Notification 2 — Approved
| Event | leave_v2.approved |
| Users/Groups in fields | u_requested_for_user |
| Subject | Your leave ${u_number} was approved |
Notification 3 — Rejected
| Event | leave_v2.rejected |
| Users/Groups in fields | u_requested_for_user |
| Subject | Your leave ${u_number} was rejected |
Step-by-step create
- Application = HR Leave. Notifications → New for each.
- Tab When to send: Event is fired + event name.
- Tab Who will receive: Users/Groups in fields → pick u_approver_user or u_requested_for_user.
- Tab What it will contain: Subject + Message HTML with ${u_number}, ${u_start_date}, ${URI_REF}.
- Submit each notification. Active = true.
Verify checklist
- Three active notifications on Leave Request table.
- Email log entry after submit/approve.
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
- Part 2: Table employee_v2, Part 4: Table bal_v2, Part 5: System Properties property.
Form fields
| Field | Value |
|---|---|
| Name | HRLX Annual Accrual |
| Application | HR Leave |
| Active | Checked |
| Run | Daily 02:00 or Yearly Jan 1 |
| Run as | System (default) |
Step-by-step create
- Scheduled Script Execution → New.
- Fill fields. Paste script below.
- 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
- Execute Now creates bal_v2 per employee without duplicate year row.
- System log: [HRLX] Annual accrual message.
Demo / test
New employee → run job → u_entitled=20, u_remaining=20.
Notifications & Jobs verify checklist
Complete after Parts 1–3.
- Three events registered on req_v2.
- Three email notifications active.
- Accrual job creates balance rows without duplicates.