Scheduled job to remove the attachedments
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2024 12:30 AM
Hi guys,
Currently, we have a daily scheduled job that removes attachments from the u_parking_permit_app table for applications created more than 30 days ago. In order to improve the scheduled job, we are required to revise the script to include an additional condition: "if the state is Completed or Canceled." This will ensure that only attachments from closed applications are removed.
Is there anyone can advise on the scripting of the additional condition (state is Completed or Canceled)?
Thank you!
var attachment = new GlideRecord("sys_attachment");
attachment.addEncodedQuery("table_name=ZZ_YYu_parking_permit_app^sys_created_on<=javascript:gs.beginningOfLast30Days()");
attachment.query();
var count = 0;
var count2 = 0;
var infoArr = [];
while(attachment.next()){
count++;
attachment.deleteRecord();
if(attachment.sys_updated_by != "system"){
var ticket = new GlideRecord("u_parking_permit_app");
ticket.addQuery("sys_id",attachment.table_sys_id+"");
ticket.query();
if(ticket.next()){
count2++;
gs.info("Ticket: "+ticket.number+", File Name: "+attachment.file_name);
var info = "Ticket: "+ticket.number+", File Name: "+attachment.file_name;
infoArr.push(info);
}
}
}
var atta = new GlideRecord("sys_attachment");
atta.addQuery("table_name","u_parking_permit_app");
atta.query();
atta.next();
gs.eventQueue("cic.del.parking.attachment",atta,JSON.stringify(infoArr),count2);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2024 04:04 AM - edited 01-10-2024 04:08 AM
Hello,
Add one more query or encoded query in loop as state is closed or canceled.
instead of this:
ticket.addQuery("sys_id",attachment.table_sys_id+"");
try this:
gr.addQuery('state', Incident_State.CLOSED);
or refer,
var now_GR = new GlideRecord('incident');
var qc = now_GR.addQuery('category', 'Hardware');
qc.addCondition('category', 'Network');
now_GR.addQuery('number','INC0000003');
now_GR.next();
now_GR.number;
gs.info(now_GR.getEncodedQuery());
If answer ids helpful please mark it as helpful or correct!
Thank You.
Br,
pratiksha.k
ThankYou

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2024 04:20 AM
Hi there,
Basically you would just enhance this part right:
ticket.addQuery("sys_id",attachment.table_sys_id+"");
So what is the issue here?
If you are not that into coding, this older article of mine might help you:
- 2019-09-04 - Article - Utilizing the breadcrumb on lists to generate your query
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field