Get a first look at what's coming. The Developer Passport Australia Release Preview kicks off March 12. Dive in! 

Create approvers on Rtim on basis of Cost Center

abdulrehman
Kilo Guru

Hey Team,

Can anyone please help me 
I created a schedule job that is generating approvers on RITM on the basis of Cost center, If the approver with level 1  doesnot approve the Ritm after 3 business days than the approval should move to the approver level 2 and so on till level 5.

the script is working good, but Im just having trouble in adjusting the calendar days into business days. The current 3 days logic is on the basis of calendar days but I want to make it on the basis of Business days. Below is the code. Thanks in advance

(function() {

var RITM_GR = new GlideRecord('sc_req_item');
//RITM_GR.addQuery('sys_id', '00a23e56fb61be50c2f6f2277befdc6b');
RITM_GR.addQuery('state', '2'); // work in progress
RITM_GR.addQuery('cat_item', 'd1d0789e1b8b201067fe8443604bcb51');
RITM_GR.query();

gs.info('Processing Access Installation RITMs...');

while (RITM_GR.next()) {

gs.info('Checking RITM: ' + RITM_GR.number);

// Get Cost Center variable value from RITM
var costCenter = getRITMVariable(RITM_GR.sys_id, 'cost_center_number');
gs.info("Cost Center for " + RITM_GR.number + ": " + costCenter);

var apprGR = new GlideRecord('sysapproval_approver');
apprGR.addQuery('sysapproval', RITM_GR.sys_id);
apprGR.orderByDesc('sys_created_on');
apprGR.query();

if (apprGR.next()) {

var apprGR1 = new GlideRecord('sysapproval_approver');
apprGR1.addQuery('sysapproval', RITM_GR.sys_id);
apprGR1.orderByDesc('sys_created_on');
apprGR1.query();
var appQuery = "";
while (apprGR1.next()) {
appQuery = appQuery + "level!=" + apprGR1.u_cs_level + "^";
}
gs.info("appQuery:" + appQuery + ":" + RITM_GR.number);

// Skip already processed approvals
if (apprGR.state == 'approved' || apprGR.state == 'rejected')
continue;

var level = 0;
gs.info("apprGR.sysapproval: " + apprGR.sysapproval);

var csapprover = new GlideRecord("x_ohs_och_approver_cost_center_approvers");
csapprover.addEncodedQuery(appQuery + "cost_center=" + costCenter);
csapprover.addActiveQuery();
csapprover.orderBy("level");
csapprover.query();

// Time difference calculation in DAYS
var created = new GlideDateTime(apprGR.sys_created_on);
var now = new GlideDateTime();
var diffDays = gs.dateDiff(created, now, true) / 86400; // seconds → days
gs.info("diffDays: " + diffDays);

if (csapprover.next()) {
gs.info("csapprover.level: " + csapprover.level + "-RITM" + RITM_GR.number);
level = csapprover.level;
}

var csapprover1 = new GlideRecord("x_ohs_och_approver_cost_center_approvers");
csapprover1.addEncodedQuery("cost_center=" + costCenter + '^level=' + level);
csapprover1.addActiveQuery();
csapprover1.orderBy("level");
csapprover1.query();

while (csapprover1.next()) {
gs.info("csapprover1 name: " + csapprover1.approver.name + "-RITM" + RITM_GR.number);

// Escalation logic (3 days)
if (diffDays >= 3) {
gs.info('Level ' + level + ' approver pending >3 days for ' + RITM_GR.number);

var nextLevel = level;
if (nextLevel <= 5) {

var approver = csapprover1.approver;

if (approver) {
var newAppr = new GlideRecord('sysapproval_approver');
newAppr.initialize();
newAppr.sysapproval = RITM_GR.sys_id;
newAppr.state = 'requested';
newAppr.level = nextLevel;
newAppr.approver = approver;
newAppr.u_cs_level = csapprover1.level;

var checkApp = new GlideRecord('sysapproval_approver');
checkApp.addEncodedQuery(
"sysapproval=" + RITM_GR.sys_id +
"^u_cs_level=" + newAppr.level +
"^approver=" + newAppr.approver
);
checkApp.query();

if (!checkApp.next()) {
newAppr.insert();
}

gs.info('Created level ' + nextLevel + ' approver for ' + RITM_GR.number);
}
}
}
}

if (!csapprover1.next() && RITM_GR.state != '3') {
if (diffDays >= 3) {
RITM_GR.state = '50';
RITM_GR.update();
}
}
}
}

gs.info('RITM approver escalation processing complete.');

function getRITMVariable(ritmSysId, varName) {
var val;
var varMtom = new GlideRecord('sc_item_option_mtom');
varMtom.addQuery('request_item', ritmSysId);
varMtom.query();
while (varMtom.next()) {
var itemOption = new GlideRecord('sc_item_option');
itemOption.get(varMtom.sc_item_option);
if (itemOption.item_option_new.name == varName) {
val = itemOption.value;
break;
}
}
return val;
}

})();

7 REPLIES 7

Utpal Dutta
Tera Guru

Hey @abdulrehman ,

Why don't you configure this approval in your flow and in the approval activity itself you can set relative 3 days after which approval will be auto cancelled.

 

Check below:

UtpalDutta_0-1767966245227.png

 

After this approval you can then set 2nd approval or more approvals.

 

If my answer helps then please mark it helpful or accept this solution.

 

Thanks,

Utpal

I cannot use flow Designer for it, I have to do this only via a schedule job as my company have created some custom functionality as a reusable flow step stuff

This will be really helpful if you can provide a script according to my schedule job. Thanks in Advance

@abdulrehman 

Did you get a chance to check my below response?

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

I read the article but this is not related to that what I am looking for.
I dont want to do with the date. I only want my schedule job to do operate in business days ,not in the calendar days.
The code I have added, its counting the calendar days, I want to create approver or cancel approval after 3 business days