The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Need a background script to cancel emails from being sent

KeithM1
Tera Expert

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();


}

1 ACCEPTED SOLUTION

Harsh Vardhan
Giga Patron

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();

}

View solution in original post

12 REPLIES 12

Sorry it was supposed to be gr.type = 'send-ignored'. I am glad that issue is resolved.

No worries.  I appreciate the help

asifnoor
Kilo Patron

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.

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.

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.