After deleting the attachment its still stored into the sys_attachment table

Sovan
Tera Contributor

We have created a custom attachment field for our record producer. When user is going to create a record with an attachment, before submitting the record first they attach the attachment and then they deleted that and again they attached a new one. 

However that deleted attachment has been stored into the sys_attachment table with prefix ZZ_YY and the new attachment has been stored properly.

 

We need to remove that deleted attachment (prefix ZZ_YY) from the sys_attachment table while submitting the record with the new attachment.

Please help to solve this.

15 REPLIES 15

@Sovan Can you post the screen shots of business rule with "When to run" and Advanced section

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

Sovan
Tera Contributor

Sovan_0-1671544641910.png

 

Sovan_1-1671544677974.png

 

@Sovan Please add this below script in business rule

 

var tableGr = new GlideRecord("sys_attachment");
tableGr.addQuery("table_name="+current.table_name.toString());
tableGr.addQuery("sys_id!="+current.sys_id.toString());
tableGr.deleteMultiple();
 
Please mark as correct answer if this solves your issue.
Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

@jaheerhattiwale Still not working.

 

Sovan_0-1671545693456.png

 

@Sovan In filter condition make "Table name is <YOU TABLE NAME>"

 

And check if table name is correct. Check for space at the end.

 

If everything is correct.

 

Then add a log  on top of the script.

gs.info("IN business rule");

 

and check if this log gets logged in logs table

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023