the first ".update();" call succeeds in my Scheduled JOB script but second one further down does not

scottbjorke
Tera Contributor
Hello! Hoping someone out there can tell me why the second of two .update() statements (using the same gliderecord object) isn't working. The first .update() works fine.
I'm running this on a PDI of Zurich. My first goal is to have a Scheduled JOB manipulate a Scheduled Report and then trigger that Scheduled Report. My second goal however is to restore the fields I had modified back to their original values before committing a subsequent .update().  This will allow the job to run multiple times and always start with the original field values in the report. This is the part that isn't working for me.
 
Give it a shot and let me know the reason and your proposed correction(s).  Thank you!
 
 
// below is the script in my Scheduled JOB (not in the Scheduled Report it modifies)
// this is extremely useful when recipients should be whoever appeared in the Scheduled Report
modifyScheduledReportParmsThenRunIt();

function modifyScheduledReportParmsThenRunIt() {
   
    var grScheduledReport = new GlideRecord('sysauto_report');
    if (!grScheduledReport.get('name', 'test Scheduled REPORT')) { // this report exists
        gs.info('cannot find report in sysauto_report table');
        return false;
    }
    // save off original values for the very end
    var orig_user_list = grScheduledReport.user_list; // GlideList
    var orig_address_list = grScheduledReport.address_list; // comma-separated list of email addresses

    // begin modifying parms of the scheduled report before triggering its execution
    // let's add foo@bar.com as a recipient of the eventual email
    if (grScheduledReport.address_list.getDisplayValue().length > 0) {
            grScheduledReport.address_list += ', '; // prep with leading comma if necessary
    }
    grScheduledReport.address_list += 'foo@bar.com';
   
    // let's add the ServiceNow User 'SlimJim' as a recipient of the eventual email
    var grUser = new GlideRecord('sys_user');
    if (!grUser.get('user_name', 'SlimJim')) {
        gs.info('cannot find "SlimJim" in sys_user table');
    } else {
        if (grScheduledReport.user_list.indexOf(grUser.sys_id) < 0) {
            if (grScheduledReport.user_list != '') {
                grScheduledReport.user_list += ', ';
            }
            grScheduledReport.user_list += grUser.sys_id;
        }
    }
    var res1 = grScheduledReport.update(); // required update prior to triggering report
    gs.info('first update returned ' + res1); // this seems to work fine

    SncTriggerSynchronizer.executeNow(grScheduledReport); // run the modified Scheduled Report

    //////////////////////////////////////////////////////////////////////////////////
    ////////////// THE BELOW PART DOESN'T WORK AND I DON'T KNOW WHY!!! //////////////
    //////////////////////////////////////////////////////////////////////////////////
    // restore original values to the Scheduled Report
    grScheduledReport.address_list = orig_address_list;
    grScheduledReport.user_list = orig_user_list;
    var res2 = grScheduledReport.update(); // call update after trigger to put everything back to original
    gs.info('second update returned ' + res2);
    // even though SN returns the sys_id, the update back to original values didn't persist -- I can still see foo@bar.com as an Email Recipient and SlimJim as a User recipient when I open the Scheduled Report to view/edit it
    return true; // last line must return a true or false
}
1 ACCEPTED SOLUTION

TejasSN_LogicX
Tera Contributor

Hi @scottbjorke ,

in your script 

TejasSN_LogicX_1-1767573785819.png

is a GlideList field, not a simple string. you’re actually storing a reference to the GlideElement, not the actual list of sys_ids.

 

and 

TejasSN_LogicX_2-1767573919620.png

this doesn’t set the field correctly, so the second .update() doesn’t persist the change..

address_list works fine because it’s just a string, but user_list needs proper handling

 

 

so you can use the toString(),Save the original user_list as a string of sys_id:

 

 

var orig_user_list = grScheduledReport.user_list.toString();
var orig_address_list = grScheduledReport.address_list.toString();

 

and the set the values 

grScheduledReport.user_list = orig_user_list;
grScheduledReport.address_list = orig_address_list;
grScheduledReport.update(); 

 

Always use .toString() to save and restore GlideList fields. Directly assigning the GlideElement won’t work, which is why your second .update() appeared to fail.

 

View solution in original post

4 REPLIES 4

d_17
Tera Expert

Hi  ,

SncTriggerSynchronizer.executeNow(grScheduledReport) runs asynchronously and re-saves the same sysauto_report record after your second update()

 

Add this gs.sleep and then update

gs.sleep(30000); // wait 30 seconds 

grScheduledReport.address_list = orig_address_list;
grScheduledReport.user_list = orig_user_list;
grScheduledReport.update();

Brad Bowman
Kilo Patron
Kilo Patron

You need to get the value of the original fields using the safer and recommended getValue, or you can force them to a string when assigning to script variables:

var orig_user_list = grScheduledReport.getValue('user_list'); // GlideList
var orig_address_list = grScheduledReport.getValue('address_list'); // comma-separated list of email addresses

- or -

var orig_user_list = grScheduledReport.user_list.toString(); // GlideList
var orig_address_list = grScheduledReport.address_list.toString(); // comma-separated list of email addresses

 

TejasSN_LogicX
Tera Contributor

Hi @scottbjorke ,

in your script 

TejasSN_LogicX_1-1767573785819.png

is a GlideList field, not a simple string. you’re actually storing a reference to the GlideElement, not the actual list of sys_ids.

 

and 

TejasSN_LogicX_2-1767573919620.png

this doesn’t set the field correctly, so the second .update() doesn’t persist the change..

address_list works fine because it’s just a string, but user_list needs proper handling

 

 

so you can use the toString(),Save the original user_list as a string of sys_id:

 

 

var orig_user_list = grScheduledReport.user_list.toString();
var orig_address_list = grScheduledReport.address_list.toString();

 

and the set the values 

grScheduledReport.user_list = orig_user_list;
grScheduledReport.address_list = orig_address_list;
grScheduledReport.update(); 

 

Always use .toString() to save and restore GlideList fields. Directly assigning the GlideElement won’t work, which is why your second .update() appeared to fail.

 

Why post the same answer 4 hours later, without adding any value, and excluding the ServiceNow recommended approach of always using 'getters and setters' getValue and SetValue to ensure proper field value retrieval and assignment?