Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

How can I query sys_email table in Scripts - Background

irenechen
Mega Contributor

Hi,

I need to extract some information from sys_email table while I am doing the production issue investigation, but when I run below scripts in the "scripts - background" tool, there's no data list out.

gs.sql("select distinct recipients from sys_email where sys_created_on= '2015-08-22' and subject like 'BCM Governance Declined%'");

Thanks in advance for your help!

Irene

12 REPLIES 12

Stijn Verhulst3
Kilo Guru

Hi Irene,



var email = new GlideRecord('sys_email');


email.addQuery('sys_created_on', '2015-08-22');


email.addQuery('subject', 'CONTAINS', 'BCM Governance Declined');


email.query();



while (email.next()) {


      gs.print(email.recipients);


}



This script can also be run in the Scripts - Background module. However; if you want to use DISTINCT, GlideAggregate needs to be used instead of GlideRecord.



More about querying via GlideRecords/GlideAggregate:



Using GlideRecord to Query Tables - ServiceNow Wiki


GlideAggregate - ServiceNow Wiki



Does this help you forward?



Kind regards,



Stijn


Hi Stijin,



Thanks your quick response. The script run without data print out.



The output screen only shows below information:


15 Script completed: script


Any other configuration need to turn on to enable the data list out?



Thanks again,



Irene


bernyalvarado
Mega Sage

Hi Irene, you can type sys_email.list in your navigation window and then you can filter down through the list of records that contain BCM Governance Declined. That will work better for your and will be a better approach to do troubleshooting in a production environment.



Thanks,


Berny


Hi Berny,



Thank you for your information. I use the query in script as the UI list


doesn't show the full string value if it's too long. I need to extract


the information from the list to generate the report.



Irene