How to remove attachment from sys_attachment table

Pradyumna Das
Tera Expert

The Attachment type variables in the portal is not behaving properly. Once you add any attachment via attachment type variable a delete button will appear but if I click on that delete button the attachment is getting removed from the portal not from the sys_attachment table. So after clicking that delete button if I submit the form then that deleted attachment is getting populated which should not happen. Is there any solution for this except calling  GildeAjax from the onchange client script. 

 

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

deleted attachment is getting populated where?

in that variable or for that RITM record?

Regards
Ankur

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

Yes in the RITM record.

Hi,

then you can have after insert BR on sc_req_item table

Condition: current.cat_item.name == 'Your Item Name'

Script:

if(current.variables.attachmentVariable == ''){
	var gr = new GlideRecord("sys_attachment");
	gr.addQuery("table_sys_id", current.getUniqueValue());
	gr.query();
	if (gr.next()) {
		gr.deleteRecord();
	}
}

Regards
Ankur

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

Community Alums
Not applicable

Hi @Pradyumna Das ,

There is an Script Include named AttachmentAjax that can be called from a Client Script using GlideAjax to perform several attachment related tasks like: list, delete, rename, etc.

GlideSysAttachment can be used to delete attachment as follows:

var attachment = new GlideSysAttachment();

var agr = attachment.getAllAttachments(<table_name>, <table sys id>);
while(agr.next()) {
attachment.deleteAttachment(agr.getValue("sys_id"));
}

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);


Mark my answer correct & Helpful, if Applicable.

Thanks,

Sandeep