schedule job script not executing, the script works in background script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2020 06:22 AM
Hi Team,
I am facing this unusual issue where a schedule job does not trigger for a schedule job, however the same works when run from background script.
The condition is something like this
var chg = new GlideRecord('change_request');
chg.addEncodedQuery('active=true^end_dateRELATIVELE@dayofweek@ago@7^state=0');
//chg.addEncodedQuery('active=true^end_dateRELATIVELE@dayofweek@ago@7^state=0^assignment_group.nameSTARTSWITHservicenow^number=CHG0041873');
chg.query(); gs.log('chg 001');
while(chg.next()){
gs.log('chg 002');
chg.state="4";
chg.close_code= 'Auto-terminated in review';
chg.close_notes ='Auto-term abandoned as change exceeded 7 days after the planned end date.';
chg.work_notes ='Auto-term abandoned as change exceeded 7 days after the planned end date.';
chg.update();
gs.log('chg 003' +chg.number);
}
This although is supposed to run everyday at 23:50 , but there is no trace in the sys_trigger table.
Also, please note when i force execute it , the logs can be seen but not the action, i.e. change does not updates/closes.
Btw, i have similar scheduled scripts running for the condition, all running in global application
var chg = new GlideRecord('change_request');
chg.addEncodedQuery('active=true^end_dateRELATIVELE@dayofweek@ago@7^state=-2');
chg.query();
while(chg.next()){
chg.state=4;
chg.close_code= 'Auto-terminated in scheduled';
chg.close_notes ='Auto-term abandoned as change exceeded 7 days after the planned end date.';
chg.work_notes ='Auto-term abandoned as change exceeded 7 days after the planned end date.';
chg.update();
}
Can you please point me towards something useful?
Regards,
Shariq
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2020 06:38 AM
Hi Shariq,
remove gs.log and use gs.info and try running
gs.log won't work in scope app and it would break the script
also check rowCount for query ; also check any query business rule on change_request table is blocking the record query
var chg = new GlideRecord('change_request');
chg.addEncodedQuery('active=true^end_dateRELATIVELE@dayofweek@ago@7^state=0');
chg.query();
gs.info('chg 001');
gs.info('Row Count is: ' + chg.getRowCount());
while(chg.next()){
gs.info('chg 002');
chg.state="4";
chg.close_code= 'Auto-terminated in review';
chg.close_notes ='Auto-term abandoned as change exceeded 7 days after the planned end date.';
chg.work_notes ='Auto-term abandoned as change exceeded 7 days after the planned end date.';
chg.update();
gs.info('chg 003' +chg.number);
}
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2020 09:59 PM
I am getting logs as i mentioned with the correct change number also. and also like i mentioned the script is in global application.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2020 02:29 AM
Hi Shariq,
can you check whether canUpdate() returns true or false
so I think the user possibly doesn't have write access to those 3 fields; can you try to update some other fields such as description or short_description
also added try catch block to handle exceptions
try{
var chg = new GlideRecord('change_request');
chg.addEncodedQuery('active=true^end_dateRELATIVELE@dayofweek@ago@7^state=0');
chg.query();
gs.info('chg 001');
gs.info('Row Count is: ' + chg.getRowCount());
while(chg.next()){
gs.info('chg 002');
chg.state="4";
chg.close_code= 'Auto-terminated in review';
chg.close_notes ='Auto-term abandoned as change exceeded 7 days after the planned end date.';
chg.work_notes ='Auto-term abandoned as change exceeded 7 days after the planned end date.';
gs.info('Can Write is: ' + chg.canWrite());
var sysId = chg.update();
gs.info('chg 003' +chg.number);
gs.info('SysId of the record updated is: ' + sysId);
}
}
catch(ex){
gs.info('Exception is: ' + ex);
}
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2020 03:08 AM
Hello Ankur,
Since, I am running as system admin, it wont matter and i would have to write roles to update the field.
I checked, and was able to get the logs too, but unable to close the change.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2020 03:22 AM
Hi Shariq,
Any before update business rule which is blocking the change to be be closed?
Can you try to use setWorkflow(false) during update to avoid triggering BR
chg.state="4";
chg.close_code= 'Auto-terminated in review';
chg.close_notes ='Auto-term abandoned as change exceeded 7 days after the planned end date.';
chg.work_notes ='Auto-term abandoned as change exceeded 7 days after the planned end date.';
gs.info('Can Write is: ' + chg.canWrite());
chg.setWorkflow(false);
var sysId = chg.update();
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader