How to remove attachment from sys_attachment table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-22-2022 08:55 PM
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.
- Labels:
-
Multiple Versions
-
Service Catalog
- 4,887 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-22-2022 09:03 PM
Hi,
deleted attachment is getting populated where?
in that variable or for that RITM record?
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-22-2022 09:09 PM
Yes in the RITM record.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-22-2022 09:12 PM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-22-2022 09:05 PM
Hi
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