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

@abdulrehman 

so you are saying you want to get difference in business days here

// Time difference calculation in DAYS

You can use schedule in your calculation

Calculate Date By Adding or Subtracting Time With A Schedule 

subtract time from schedule to update task due date 

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

@abdulrehman 

Hope you are doing good.

Did my reply answer your question?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

Ankur Bawiskar
Tera Patron

@abdulrehman 

here is blog to add business days to particular date, enhance it for your requirement

Add Business Days 

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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