background script update not updating more than a record or two

wpatrickhames
Tera Guru

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.

1 ACCEPTED SOLUTION

wpatrickhames
Tera Guru

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.  


View solution in original post

13 REPLIES 13

I have.   I removed them to streamline the code for this post.


Cool. Yeah. Running the routine through a schedule job should get you going.



Please keep us posted if it was successful.



Thanks,


Berny


Chuck Tomasi
Tera Patron

Are you hitting a null value like in the article you cited?


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.


markus_schaer
Tera Guru

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