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.

Delete attachments button on incident form

Garry Knight
Kilo Contributor

Hi,

I am trying to create a button on the incident form which will delete all attachments currently on the incident. I am trying to do this as a UI action but am not having much luck.

I have put the button there and set up the permissions but can't seem to get the function to work. Could anyone advise if this is possible?

Thank you.

1 ACCEPTED SOLUTION

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Garry,



Here is the optimized code you can use.


var attach = new GlideSysAttachment();


attach.deleteAll(current);


current.update();


View solution in original post

15 REPLIES 15

Hi Srinivas,



This works however it does not save changes made to the form when clicked. Is there a way to get it to do this in a similar way to how the update button works?


add current.update() at the end of the code


In that case, Here is the update code.


removeAttachments();


function removeAttachments(){  


var gr=new GlideRecord('sys_attachment');  


gr.addQuery('table_sys_id', current.sys_id);  


gr.query();  


while(gr.next()){  


gr.deleteRecord();  


}}


current.update();


Thank you both it works fine,



Is there a way to get it to stay on the opened form after it is clicked though instead of going back to the list?



Thanks again.


add action.setRedirectURL(current); at the end of the code.