- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-02-2020 07:43 AM
I've created the below script to cancel emails that are in the queue in a ready state in order to keep them from being sent out. When I tested it in DEV, it changed the state to ignored but then it still processed the emails when I turned on email send/receive. I don't have access to the test mailbox so I don't know if they actually got processed or not or if they were ignored. I do know that they were not in the skipped emails.
Here's the script. Any idea where I went wrong?
var gr= new GlideRecord('sys_email');
gr.addEncodedQuery("mailbox=outbox^state=ready^sys_created_on<=javascript:gs.dateGenerate('2020-09-02','23:59:59')");
gr.query();
while(gr.next()){
gr.setWorkflow(false); //prevent running business rule
//gs.print('Created on ' + gr.sys_created_on + "State: " + gr.state);
gr.state='ignored';//cancelled state
gr.update();
}
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-02-2020 08:20 AM
if still not working, can you give a try with below script. this should work.
var gr= new GlideRecord('sys_email');
gr.addEncodedQuery("mailbox=outbox^state=ready^sys_created_on<=javascript:gs.dateGenerate('2020-09-02','23:59:59')");
gr.query();
while(gr.next()){
gr.state='ignored';//cancelled state
gr.type='send-ignored';
gr.setWorkflow(false); //prevent running business rule
gr.update();
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-02-2020 09:08 AM
Sorry it was supposed to be gr.type = 'send-ignored'. I am glad that issue is resolved.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-02-2020 09:13 AM
No worries. I appreciate the help

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-02-2020 07:54 AM
Hi
The script looks fine. Just verify if its entering your while loop or not.
Also check the logs and see if you are getting any error when setting to ignored.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-02-2020 08:28 AM
I didn't see any errors in the logs. I figure it must be entering the while loop because it is setting the state for all the emails to ignored.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-02-2020 08:31 AM
Okay. A quick fix for this could be to put a test email address under email properties and process all the mails. Then remove the test user mail.