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

Harsh Vardhan
Giga Patron

try now

 

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.setWorkflow(false); //prevent running business rule
gr.update();

}

make sure you added correct state value 

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Keith,

Below is the updated script.

 

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='send-ignored';//cancelled state
gr.update();


}

I tried your script and it didn't work.  It set the state to send-ignored but it was in blue because there is no state of send-ignored.  When I turned the notifications back on, they still sent