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

I'm using this in a Business Rule, on the sysapproval_approver table.   So not on a scoped app, no.


No worries though, as my solution (work-around) is working great.


I just wasn't understanding why Brad's solution was throwing an error for me.


Thanks for your reply though!  


Hi Brad. What is exactly the recordGR? The sys_id of the request where the documents had been attached?

janpost
Kilo Contributor

Thank you! deleteAttachment(...) and deleteAll(...) are the functions I was looking for.


Jan,


For the benefit of future searchers (aka me!), would you mind marking Brad's post as the answer?



Thanks!


Ben Rowny
Tera Guru

Anyone stumbling across this today will find deleteAll does not work on the scoped version of GlideSysAttachment - and going straight to the sys_attachment table has issues with cross-scope policy / application access.

You can just use deleteAttachment in the scoped version of GlideSysAttachment, like so:

function deleteAllAttachments(record){
	// Utility function removes all the previous attachments
	// Must use GlideSysAttachment in scope
	var gsa = new GlideSysAttachment();

	var att = new GlideRecord('sys_attachment');
	att.addQuery('table_name',record.getTableName());
	att.addQuery("table_sys_id",record.sys_id);
	att.query();
	while(att.next()){
		gsa.deleteAttachment(att.sys_id);
	}
}

I believe this should also respect whether your current scope can access the table your attachments are attached to, which is important, and keeps security tight on your sys_attachment table.