Need advice, Background script works fine in test but does not return results in production.

JR42
Giga Guru

Hi, I am trying to run the script below in my production environment, however, once the Running Transaction window closes, there are no results, and no link to the 'script execution history and recovery', just a blank window. 

In the lower environments, the script runs and prints the results just fine. 

It takes approximately 1200 seconds for the Transaction to run in production for 1 day.

 

Note: I have the same results when running for 1 day, 1 week, 1 month, 3 months.

 

Script:

var gr = new GlideRecord('sysevent_email_action');
gr.addEncodedQuery('active=true');
gr.query();
while(gr.next())
{
var count = 0;
// gs.print(gr.name);
var gr1 = new GlideRecord('sys_email_log');

gr1.addEncodedQuery('sys_created_onONLast day@javascript:gs.daysAgoStart(1)@javascript:gs.daysAgoEnd(0)');
gr1.query();
while(gr1.next())
{
//gs.print("gr: " +gr.sys_id);
//gs.print("gr1 :"+gr1.sys_id);

if(gr.sys_id == gr1.notification)
{
count++;
}
}
if(count!=0)
gs.print("Notification name: "+ gr.name + " Count: "+ count);

}

 

Any tips or advice to get this to print the output like it does in the dev/test environments would be greatly appreciated. 

 

 

Results in test:

Justin3_0-1703779401444.png

 

Results in Production

Justin3_1-1703779456976.png

 

7 REPLIES 7

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @JR42 

 

Is any access blocking? Try to add some info messages to see till what its executed. or log a HI case.

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Anubhav24
Mega Sage
Mega Sage

Hi @JR42 ,

As already mentioned you can write log statements to see till where the code executes in prod and for testing use setLimit in prod so that you can be sure that records are returned in prod environment and then we can help troubleshoot from there.

Isaias H
Tera Guru

Hi @JR42 ,

 

Is there a reason why you need to get this information with a script?

 

You can create a report directly in the "sys_email_log" table, and dot-walk the "notification" field where active is true.

 

Also, you can use GlideAggregate in the same "sys_email_log" table then group/count it by "notification". There is no need for two queries because the "sys_email_log" record already has a reference to the "sysevent_email_action" table.

 

 

var agg = new GlideAggregate('sys_email_log');

var encodedQuery = 'sys_created_onONYesterday@javascript:gs.beginningOfYesterday()@javascript:gs.endOfYesterday()^notification.active=true';
agg.addEncodedQuery(encodedQuery);

// Group the records by the 'notification' field
agg.addAggregate('COUNT', 'notification');
agg.groupBy('notification');
agg.orderByAggregate('COUNT', 'notification');
agg.query();

// Iterate over the results and print the count for each notification
while (agg.next()) {
    var notification = agg.notification.getDisplayValue();
    var count = agg.getAggregate('COUNT', 'notification');

    gs.info('Notification: ' + notification + ', Count: ' + count);
}

 

 

I hope my response has been helpful. If so, please consider marking it as such. Thank you!

Hi Isaias, thanks for this advice.  I'm trying to use your suggestion of reporting on the sys_email_log table.
I ran into an issue, I need to filter by date, say, last 6 months.  However, in the report form for sys_email_log, when I add a condition for Created, the drop down value is empty.  Do you have any ideas on why this would be happening?

2024-02-12 09_49_31-ServiceNow Where Service Never Ends.png

If I go directly to the sys_email_log.list, I can add the condition without an problem.