Background script is updating the values but not through Scheduled jobs.

SR2
Tera Contributor

Hello everyone.

I have an issue regarding Scheduled jobs, the below script is running fine in business rule and with background Scripts but not with scheduled jobs, can you please help me with the below script. It is used to update the 'u_sla_indicator' field on 'core_company' table by looking all the 'task_sla's' of the core_company table which are 'in_progress'. 

Scheduled jobs script:

var gr = new GlideRecord('task_sla');
gr.addQuery('stage', 'in_progress');
gr.addEncodedQuery('taskSTARTSWITHVRA^ORtaskSTARTSWITHIRQ^stage=in_progress');
gr.query();
while (gr.next()) {
var tableName = gr.task.getDisplayValue();
if (tableName.indexOf('VRA') > -1 || tableName.indexOf('IRQ') > -1) {
var percentages = [];
var irq = [];
var assessment = [];
var wen = 0;
var core = new GlideRecord('core_company');
core.addEncodedQuery('vendor=true^statusINin_scope,exempt');
core.query();
while (core.next()) {
if (core.vendor == true && (core.status == 'in_scope' || core.status == 'exempt')) {
var IRQ = new GlideRecord('sn_vdr_risk_asmt_vdr_tiering_assessment');
IRQ.addQuery('vendor', core.sys_id);
IRQ.query();
while (IRQ.next()) {
irq.push(IRQ.sys_id.toString());
}
var assessments = new GlideRecord('sn_vdr_risk_asmt_assessment');
assessments.addQuery('vendor', core.sys_id);
assessments.query();
while (assessments.next()) {
assessment.push(assessments.sys_id.toString());
}
var task = new GlideRecord('task');
task.addEncodedQuery('sys_idIN' + irq + ',' + assessment);
task.query();
while (task.next()) {
var tskSLA = new GlideRecord('task_sla');
tskSLA.addQuery('task', task.sys_id);
tskSLA.addQuery('stage', 'in_progress');
tskSLA.query();
while (tskSLA.next()) {
percentages.push(tskSLA.getValue("percentage"));
}
}
var max = Math.max.apply(null, percentages);
if (max > 0) {
if (max < 50) {
wen = 'Green – Below 50% Elapsed';
}
if (max >= 50 && max <= 75) {
wen = 'Yellow - Between 50% to 75% Elapsed';
}
if (max >= 75 && max <= 100) {
wen = 'Orange – Between 75% to 100% Elapsed';
}
if (max > 100) {
wen = 'Red - Above 100% Elapsed';
}
} else {
wen = 'No Active SLAs';
}

var company = new GlideRecord('core_company');
if (company.get(core.sys_id)) {
company.u_sla_indicator = wen;
company.update();
}
}
}
}
}

 

 

Thankyou.

7 REPLIES 7

SatyakiBose
Mega Sage

Hello @SR2 

Can you please check if the Run as field for the scheduled job is set properly.

For any scheduled job to execute, the Run as field should have a user name, that is active, and has admin access.

SR2
Tera Contributor

Hello @SatyakiBose , I have set the run as field to 'System Administrator, GRC'.

Can you check if the user profile has the admin access at global scope or application scope level.

Ankur Bawiskar
Tera Patron
Tera Patron

@SR2 

Did you try to add gs.info() at proper places to debug?

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