Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

current.update() is not working in my Scheduled Report

scottbjorke
Tera Contributor

Using Yokohama release, and I have the following roles: itil, report_admin, report_publisher, report_scheduler, report_user, and schedule_admin.

 

I have a Scheduled Report that aims to dynamically modify the email message body to display any 'hits' found within a separate Glide Record of Change records. I want the outbound email to list any CHG numbers where Requested by = "John Doe". (Note: The report I associated with my Scheduled Report returns plenty of Change records, and many are confirmed to have been Requested By "John Doe".) The report is mine, and the Scheduled Report is set to "Run As" me.

 

As part of my Scheduled Report condition, I record each 'hit' by adding it to the outbound email message body. Lastly, I add current.update(); so that any updates to the message body earlier in the script will become part of the outgoing email. However, when the script runs, the email finally arrives but it still shows only the original message body without any of the scripted body updates. Since receiving an email means the entire script evaluated to true, maybe something 'tried' to happen.

In years past (prior to Orlando release) I had successfully been able to do this in dozens of different scheduled reports. Wondering if:

- insufficient role? perhaps roles were split up for security reasons starting with Orlando release?
- perhaps ServiceNow platform upgrade deprecated .update() within Scheduled Reports?
- perhaps this can be done only using a Scheduled *Job* rather than a Scheduled *Report*? <-- asking because I do not have access to System definition > Scheduled Jobs.
 
On the topic of roles, I do not wish to request full "admin" role. I'm sure that will be shot down immediately. However, if the only way the above could work is to obtain "admin" role, I would much rather ask our ServiceNow admins to create a new Group that had the role but also includes a bunch of ACL's to dumb it down and limit use to stuff like this only. Advice on this would also be appreciated.
 
Any ideas, knowledge, or help here by the greater community is very much appreciated in advance! Please forgive me if I missed something blatantly embarrassing!
1. Screenshot of the header
scottbjorke_0-1761933386634.png

 

2. Here is the script in my Scheduled Report form:

answer = DidJohnDoeRequestAnyChanges();
function DidJohnDoeRequestAnyChanges() {
    current.report_body = '<p>START'; // initialize the body of the outbound email including HTML
    var ChangeNumberArray = new ArrayUtil(); // use this to determine if any hits
    // create a local GlideRecord that's exactly like the chosen Report that we can loop through
    var gr = new GlideRecord(current.report.table); // borrow from chosen Report
    gr.addEncodedQuery(current.report.filter); // borrow from chosen Report
    gr.query(); // gr now contains its own 'copy' of the borrowed report that we can loop through
    while (gr.next()) {
        if ( gr.requested_by.getDisplayValue() == 'John Doe' ) {
            // found one -- display it within the outbound email body and add it to my array
            current.report_body += ('<br>Found ' + gr.requested_by.getDisplayValue() + ' on ' + gr.number);
            ChangeNumberArray.push(gr.number);
        }
    }
    if ( ChangeNumberArray.length > 0 ) {
        current.report_body += '<br>DONE</p>';
        current.setWorkflow(false); // saw advice to include this before .update()
        current.update(); // this update is not working -- the outbound email body still shows only the original
        // insufficient role? NOW platform upgrade? deprecation of .update() within Scheduled *Report* (ie, must instead use Scheduled *Job*)?
        current.setWorkflow(true); // saw advice to include this before .update()
        return true;
    } else {
        return false;
    }
}
 3. Screenshot of the footer showing original email message body
scottbjorke_1-1761933481392.png

Theories are welcome, but verified solutions are REALLY welcome!  THANK YOU, community!

Scott Bjorke

scott.bjorke@gmail.com

3 REPLIES 3

SVimes
Kilo Sage

If my understanding is correct, what you are doing just will not work. The Condition field should simply evaluate to true or false to determine whether or not to run the scheduled execution of emailing the report referenced.

 

However, the introductory message can be used to run mail scripts for that dynamic content as discussed here. Just allow your Condition to evaluate to T/F and move all of the dynamic content portion into the mail script.

 

Please correct my understanding if I've missed something.

Sable Vimes - CSA

That’s a very interesting option. However, as indicated in my original messsge, I don’t have the required “admin” role , so I cannot create email scripts.

What I’m referring to is something had worked fine for years until Orlando was released. Hoping the solution simply shifted elsewhere within the tool rather than needing absolute "admin" access.

Great idea though. Thank you for the education!

Scott

Ranjane_Omkar
Kilo Sage

@scottbjorke,

 

I think the report generation and the creation of the initial email occur before your conditional script is executed. 

 

Regards

----

If this response was helpful, please select "Accept as Solution" and "Helpful." This helps both the community and me.