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

bernyalvarado
Mega Sage

Hi Patrick



Try running it as an on demand scheduled job



Thanks,


Berny


Correct. Create one for running a script and making it for on demand execution. You copy your code over there and then hit execute.



Thanks,


Berny


bernyalvarado
Mega Sage

You may want to include some gs.log in the middle if you want to see the progress that is doing and a gs.log at the end so that you can check through the logs when it ends.



Thanks,


Berny