Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Remove attachments that are 30 days old

Jesus Nava
Tera Guru

Hello experts, please I need your help, I need to create a scheduled job that will delete all attachments except the content type that is not an image.

I got this from another post:

JesusNava_0-1679597646531.png

tested it with a single incident, used the sys_id from the incident and worked.

this is how I tested it:

JesusNava_1-1679597678750.png

But I need to include in the script the condition to remove all types of attachments except the ones that include images and that were created 7 days ago.

how can I insert this condition?

Thank you

1 ACCEPTED SOLUTION

Yousaf
Giga Sage

Hi jesus,
You can try this in your Scheduled script

 

var gr = new GlideRecord('sys_attachment');
gr.addEncodedQuery('table_nameLIKEincident^content_typeNOT LIKEimage^sys_created_onRELATIVEGT@dayofweek@ago@7');
gr.query();
gr.deleteMultiple();

 

 

Mark Correct and Helpful if it helps.


***Mark Correct or Helpful if it helps.***

View solution in original post

3 REPLIES 3

Yousaf
Giga Sage

Hi jesus,
You can try this in your Scheduled script

 

var gr = new GlideRecord('sys_attachment');
gr.addEncodedQuery('table_nameLIKEincident^content_typeNOT LIKEimage^sys_created_onRELATIVEGT@dayofweek@ago@7');
gr.query();
gr.deleteMultiple();

 

 

Mark Correct and Helpful if it helps.


***Mark Correct or Helpful if it helps.***

Thank you Yousaf it worked this way!

Regards

Sure No Problem.
Glad! it worked


***Mark Correct or Helpful if it helps.***