HR Leave · Australia

Script — inbound_scripted_rest_leave.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 Studio Business Rules Client Scripts Script Includes

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

Source: src/server/rest/inbound_scripted_rest_leave.js

/**
 * Application: Custom · Scope: x_786686_hr_leave · sys_id: c84db0e8839f0b509f02b0d6feaad3c9
 * Scripted REST API Resource: Create Leave (Inbound)
 * API ID:        hrlx_api_v2
 * Resource:      POST /leave
 * Full path:     /api/x_786686_hr_leave/hrlx_api_v2/leave
 *
 * TEST FIRST (no Postman until this works):
 *   All > System Web Services > REST API Explorer  ($restapi.do)
 *   Namespace: x_786686_hr_leave | API: hrlx_api_v2 | Resource: leave | POST
 *   Request body: { "employee_id":"EMP001", "leave_type":"annual", ... }
 *   Click Send → expect 201 → Copy as cURL
 *
 * Simpler alternative (Table API, no script):
 *   Explorer → namespace now → Table API → x_786686_hr_leave_req_v2 → POST
 *   See: src/server/rest/rest_api_explorer.md
 *
 * Create API record:
 *   All > System Web Services > Scripted REST APIs > New + Resource POST /leave
 */
(function process(request, response) {

    var data = request.body && request.body.data ? request.body.data : {};

    // Validate
    if (!data.employee_id) {
        response.setStatus(400);
        return response.setBody({ error: 'employee_id is required' });
    }
    if (!data.start_date || !data.end_date) {
        response.setStatus(400);
        return response.setBody({ error: 'start_date and end_date are required' });
    }

    // Create via integration utility
    var sysId = new x_786686_hr_leave.HRLXProIntegrationUtilsV2().inboundCreateLeave(JSON.stringify(data));

    if (!sysId) {
        response.setStatus(422);
        return response.setBody({ error: 'Could not create leave request (employee not found?)' });
    }

    response.setStatus(201);
    response.setBody({ sys_id: sysId, message: 'Leave request created' });

})(request, response);