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 tried that, and made some changes to the query above.   It's still not working.   I'm sure there's a reason why these records are not updating, but I haven't been able to find it.


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.  


Hi Patrick,



Do you tried the scheduled job? There was one time (some releases ago ) where I had this big CMDB clean up script. I did tried it out using a Fix script but it halted/skipped the execution on some records. Then I tried the scheduled job and it worked like a charm!



That's why i often recommend running this type of routines in a scheduled job.



Thanks,


Berny


I DID try the scheduled job.... right after you suggested it.   It hung up the way the background script did.   However when I ran the fix script, it worked like a charm.  



That's odd that we had the same issues but success with the opposite methods.  



I do appreciate you following up, though.   Thanks.