I want to delete the attachment of RITM if I delete the attachment from REQ. I have already used some code to copy attachments of RITM and REQ.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2022 04:43 AM
I had a requirement where I had to copy the attachments of RITM into REQ. And also If I delete the attachment from REQ the attachments from the respective RITM should also get deleted.
I have applied some Business Rule but it is deleting the other attachments which are getting attached.
var gr = new GlideRecord("sys_attachment");
var req = new GlideRecord('sc_request');
if (req.get(current.table_sys_id)) {
gr.addEncodedQuery("table_name=sc_req_item" + "^file_name=" + current.file_name);
gr.query();
while (gr.next()) {
gr.deleteRecord();
}
}
Can somebody help with the code?
- Labels:
-
Service Catalog
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2022 04:49 AM
Hi,
you should query RITM then
I assume your above script is in Before Delete BR on sys_attachment and runs when file gets deleted from REQ
var ritm = new GlideRecord('sc_req_item');
if(ritm.get('request', current.table_sys_id)){
var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_sys_id", ritm.getUniqueValue());
gr.query();
while(gr.next()){
gr.deleteRecord();
}
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2022 09:26 PM
Thank you for the reply. But, if I'm using this code then what is happening is if I try to add more then one attachments to the RITM record the previous attachment gets deleted and I'm left with only one recent attached attachment.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2022 10:12 PM
Hi,
the BR would run on before delete on Attachment table and when file is deleted from REQ record
can you share your BR configuration screenshots and script and how are you testing it
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2022 10:59 PM
Testing Steps:- 1. Attached a file in RITM
2. the Attachment gets attached to the respective REQ
3. When I'm adding a second attachment in RITM, then it doesn't get the attachment as well as the previous attachment gets deleted.
I have used this code for copying the attachments from RITM to REQ