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.

Mayank Gupta8
Tera Contributor

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?

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

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

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

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.

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

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

 

find_real_file.png

 

Testing Steps:- 1. Attached a file in RITM find_real_file.png

2. the Attachment gets attached to the respective REQ find_real_file.png

 

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

https://community.servicenow.com/community?id=community_article&sys_id=b86708301b755c907a5933f2cd4bc...