Background script is updating the values but not through Scheduled jobs.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-15-2023 07:12 AM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-15-2023 07:35 AM
Hi,
In the future, if you don't mind, please use the "Insert/Edit code sample" button when posting code on the forums so that it remains organized and easier to read.
Can you give a bit more information as to how it's not working? Do you mean it's not running at all? It is, but isn't doing what you expect? Have you added log statements to the schedule job and see what you're getting back, etc?
It could be that your scheduled job isn't firing when you think it should and the settings are off slightly.
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-16-2023 01:38 AM
Thanks for your reply,
The scheduled isn't running at all, I tried with gs.info() and it is not throwing any message in it.
when I try the same script in background script by taking a single record and all the records, then it's working fine.
Thankyou.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-16-2023 06:19 AM
Hello,
If the schedule is not running at all, then at this point, it isn't the script causing the problem. You'd need to check your settings with the schedule job and ensure it's all correct. You mention it working in background, etc., but if your log statements aren't showing, and they're placed properly (such as one at least in the very, very beginning of your script) and it's not showing...then it's a scheduled job setting issue or your interpretation of those settings, that may be causing some confusion/causing it not to work.
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!