1. receive json payload from workday
2. check 'User name' in the payload to match the 'unique number' in user table
3. If a matching user is found , check the offbaording lifecycle event created or not for the user
4. If already created 'offbaording', check the rescinded status in payload
5. If rescinded is yes enable rescined for 'offbaording'
6. If rescinded is no and transction status is cancelled in payload cancel 'offbaording' event
7. If user found but offbaording lifecycle even not created generate new 'offboarding' request based user country region like India, international, Australia
if country is Amesrica = HR Offboarding Event -A
if country is International,global & none = HR Offboarding Event -B
if country is Australia = HR Offboarding Event - C
then move to the next user in payload or skip to the next if 'unique number' doesn't match
if the process is successfull for all users, respond with a status is 500
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
// Parse the JSON body from the request
var payload = request.body.data;
// Initialize response object
var responseStatus = {
status: 'success',
message: 'Offboarding processed successfully for all users.'
};
// Loop through each user in the JSON payload
for (var i = 0; i < payload.length; i++) {
var user = payload[i];
var userName = user['User Name'];
var email = user['Email - Primary Work'];
var payThrough_date = user['Pay Through Date'];
var terminationDate = user['Termination Date'];
var dayTime_completed = user['Date and Time Completed'];
var dayTime_initaited = user['Date and Time Initiated'];
var rescinded = user['Rescinded'];
var transactionStatus = user['Transaction Status'];
// Ensure we have the necessary fields in the JSON
if (!user.userName) {
responseStatus.status = 'error';
responseStatus.message = 'User Name is missing';
break;
}
// Look up user by the identi field in ServiceNow (sys_user table)
var userGR = new GlideRecord('sys_user');
userGR.addQuery('u_unique_id', user.userName); // Assuming identi is a field in sys_user
userGR.query();
// If user is found, process the offboarding
if (userGR.next()) {
// Check the lifecycle event for offboarding
var offboardingGR = new GlideRecord('sn_hr_le_case'); // Assuming 'offboarding' is the table for lifecycle events
offboardingGR.addQuery('opened_for', userGR.sys_id);
offboardingGR.addEncodedQuery('hr_service.nameINHR Offboarding Event - A,HR Offboarding Event - B,HR Offboarding Event - C')
offboardingGR.query();
// If an offboarding event is found
if (offboardingGR.next()) {
// If offboarding has been rescinded
if (user.rescinded === 'yes') {
// Rescind offboarding (This could be custom logic to "rescind" the offboarding)
offboardingGR.triggerRescind(); // Example function to rescind offboarding
gs.info('Offboarding rescinded for user ' + user.identi);
}
// If transaction status is cancelled
else if (user.transaction_status === 'cancelled') {
// Cancel offboarding (Custom logic to cancel offboarding)
offboardingGR.cancel(); // Example function to cancel offboarding
gs.info('Offboarding cancelled for user ' + user.identi);
}
} else {
// If no offboarding event is found, create a new one based on country/region
var hrProfile = new GlideRecord('sn_hr_core_profile'); // Assuming this comes from the payload
hrProfile.addQuery('user', userGR.sys_id);
hrProfile.query();
if (hrProfile.next()) {
if (hrProfile.x_sanag_hrsd_region == 'india') {
var cart = new Cart();
var item = cart.addItem('f05a3c9c8763575');
cart.setVariable(item, 'subject_person', hrProfile.user.sys_id);
cart.setVariable(item, 'last_paythrough_date', payThrough_date);
cart.setVariable(item, 'last_working_day', terminationDate);
var cartGR = cart.getCart();
cartGR.requested_for = current.caller_id;
cartGR.update();
var rc = cart.placeOrder();
} else if (hrProfile.x_sanag_hrsd_region == 'Australia') {
var cart1 = new Cart();
var item1 = cart.addItem('f9fb41ab879d9e');
cart1.setVariable(item, 'subject_person', hrProfile.user.sys_id);
cart1.setVariable(item, 'last_paythrough_date', payThrough_date);
cart1.setVariable(item, 'last_working_day', terminationDate);
var cartGR1 = cart.getCart();
cartGR1.requested_for = current.caller_id;
cartGR1.update();
var rc1 = cart.placeOrder();
} else if (hrProfile.x_sanag_hrsd_region == 'International' ||
hrProfile.x_sanag_hrsd_region == 'Global') {
var cart2 = new Cart();
var item2 = cart.addItem('8a39fcd887695690cf10ca690cbb354f');
cart2.setVariable(item, 'subject_person', hrProfile.user.sys_id);
cart2.setVariable(item, 'last_paythrough_date', payThrough_date);
cart2.setVariable(item, 'last_working_day', terminationDate);
var cartGR2 = cart.getCart();
cartGR2.requested_for = current.caller_id;
cartGR2.update();
var rc2 = cart.placeOrder();
}
}
}
} else {
// If no user is found with the identi field, skip to the next user in the payload
gs.info('User with identi ' + user.identi + ' not found in ServiceNow.');
continue; // Move to the next user in the JSON payload
}
}
// Send final response
if (responseStatus.status === 'success') {
response.setStatus(500); // If successful for all users, set status to 500
} else {
response.setStatus(400); // If an error occurred, set status to 400
}
response.setBody(responseStatus);
})(request, response);