- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-10-2017 06:49 AM
I'm attempting to run a background script to update some duration values on the sc_req_item table (calendar_duration and business_duration fields). Below is the query that I am trying to run. However, when I run it using background scripts, it updates a record, sometimes two or three and then craps out. However if I comment out line 17 and use a gs.log to print out the values that I calculate on lines 11 and 15, I get rows and rows of data.
When searching the community, I found this item (Background Script not updating all returned Glide Records ), which is an issue that is similar to mine. Is this a legit bug? If so, what's the alternative way to run this script in order to update the duration values?
var grRI = new GlideRecord('sc_req_item');
grRI.addEncodedQuery('state=3^ORstate=5^ORstate=7'); // states 3,5,7 (closed complete, closed rejected, closed cancelled);
//grRI.addQuery('business_duration','NULL');
//grRI.addQuery('calendar_duration','NULL');
grRI.query();
while (grRI.next()) {
// Set calendar duration
grRI.calendar_duration = gs.dateDiff(grRI.sys_created_on.getDisplayValue(),grRI.closed,false);
// Set business duration
var dur = calcDurationSchedule(grRI.sys_created_on, grRI.sys_updated_on);
grRI.business_duration = dur;
grRI.update();
}
function calcDurationSchedule(start, end) {
// Get the user
var usr = new GlideRecord('sys_user');
usr.get(gs.getUserID());
// Create schedule - BCBSLA 8-5 Weekdays schedule and pass in the users timezone
var sched = new GlideSchedule('97fb0a3e87252100e08f276709434dfc',usr.time_zone);
// Get duration based on schedule/timezone
return (sched.duration(start.getGlideObject(), end.getGlideObject()));
}
Thanks for any help you can offer.
Solved! Go to Solution.
- Labels:
-
Best Practices
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-10-2017 10:43 AM
Thanks to everyone for your help. I was able to get the columns to update finally. For some odd reason, the only way to get the script to work properly was to use a Fix Script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-10-2017 07:12 AM
I have. I removed them to streamline the code for this post.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-10-2017 07:15 AM
Cool. Yeah. Running the routine through a schedule job should get you going.
Please keep us posted if it was successful.
Thanks,
Berny

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-10-2017 06:57 AM
Are you hitting a null value like in the article you cited?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-10-2017 07:12 AM
I assume they are NULL because when I have the whole table list and I go to those two columns where there is no data, I select "Show matching". When the screen refreshes, the filter value box for those two columns show =NULL. I simply want to update the rows where there is nothing in those field values.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-10-2017 07:43 AM
Hi Patrick
Do you have an old Business Rule running on the Requested Item [sc_req_item] table, which also uses a GlideRecord variable named "gr", not enclosed inside a function?
Here is a legacy wiki article explaining the issue: http://wiki.servicenow.com/index.php?title=Business_Rules_Best_Practices#Enclose_Code_in_Functions
The fastest approach to test this, is to rename your variable "gr" to something more unique (e.g. "grReqItem") and run the script again.
This issue does not apply to newer Business Rules, since they are by default enclosed within a function: https://developer.servicenow.com/app.do#!/training/article/app_store_learnv2_scripting_jakarta_serve...
BR Markus