Remove attachments using GlideSysAttachment

janpost
Kilo Contributor

Is it possible to remove attachments by using GlideSysAttachment? I'm looking for a function similar to GlideSysAttachment.copy(...), something like GlideSysAttachment.remove(...) or GlideSysAttachment.delete(...). If there is such a function where is it documented?

1 ACCEPTED SOLUTION

Brad Tilton
ServiceNow Employee
ServiceNow Employee

If you want to remove all attachments from the record, you can use the following where recordGR is the gliderecord representing the record from which you want to remove attachments.



var attach = new GlideSysAttachment();


attach.deleteAll(recordGR);


View solution in original post

15 REPLIES 15

Hi Brad,



But i'm unable to delete my attachments in task using this ,



var task = new GlideRecord('sc_task');


task.addQuery('request_item',current.table_sys_id);


task.query();


while(task.next()) {


gs.log("task.sysid" + task.sys_id);


var attach = new GlideSysAttachment();


attach.deleteAll(task.sys_id);


GlideSysAttachment.copy('sc_req_item', current.table_sys_id, 'sc_task', task.sys_id);


}



I'm writing this BR on sys_ attachment.


Condition builder current.table_name == 'sc_req_item'



I would like to delete all my old attachments in task and copy all my attachments from RITM everytime i add attachment to RITM




It appears that you're passing the "deleteAttachments" a sys_id string instead of a GlideRecord object. Try this instead...


Old code:


var attach = new GlideSysAttachment();


attach.deleteAll(task.sys_id);



New code:


var attach = new GlideSysAttachment();


attach.deleteAll(task);


Odd.. I get this:



Error running business rule 'Attachments for Approval Emails (Claims)' on sysapproval_approver:Created 08-19-2016 09:50:52:AM, exception: org.mozilla.javascript.EvaluatorException: Can't find method com.glide.ui.SysAttachment.deleteAll(string). (; line 243)



find_real_file.png



find_real_file.png


So I'm using this instead.



find_real_file.png


David,


Are you using this in a scoped app? If so, the available APIs are different from the global application scope (and very limited I might add).



There is a "deleteAttachement" method, but since you have to query for the attachments manually (like you did in your solution below), it doesn't make a whole lot of sense to bother with it.