Attachment removal from Portal removes only RITM attachments, not SCTASK
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
8 hours ago
Hi Team,
I’m working on an enhancement related to attachment handling in ServiceNow Service Portal.
Current Behavior :
When a user deletes an attachment from the Service Portal (on a Requested Item – RITM):
- The attachment is removed only from the RITM
- However, the same attachment (if copied or added earlier) remains on related Catalog Tasks (SCTASK)
Expected Behavior :
- When an attachment is deleted from the RITM via Portal:
- It should also be deleted from all related SCTASK records
- Work Notes should be added to:
- RITM (indicating deletion)
- SCTASK (indicating synchronized removal)
How can we develop the solution
Regards,
B Siva Teja
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
43m ago
Using a before Delete Business Rule you can achieve it.
1.
- Name: Synchronize Attachment Deletion to SCTASKs
- Table: Attachment [sys_attachment]
- Advanced: Checked
- When: Before
- Delete: Checked
- Condition: current.table_name == 'sc_req_item'
Sample code: not tested
(function executeRule(current, previous /*null when async*/) {
var sctask = new GlideRecord('sc_task');
sctask.addQuery('request_item', current.table_sys_id);
sctask.query();
while (sctask.next()) {
var att = new GlideRecord('sys_attachment');
att.addQuery('table_name', 'sc_task');
att.addQuery('table_sys_id', sctask.sys_id);
att.addQuery('file_name', current.file_name);
att.addQuery('content_type', current.content_type);
att.addQuery('size_bytes', current.size_bytes);
att.query();
if (att.next()) {
var attachSysID = att.sys_id;
att.deleteRecord();
var taskNote = new GlideRecord('sc_task');
taskNote.get(sctask.sys_id);
taskNote.work_notes = 'Synchronized Removal: Attachment "' + current.file_name + '" was deleted because it was removed from the parent RITM.';
taskNote.update();
}
}
var ritm = new GlideRecord('sc_req_item');
if (ritm.get(current.table_sys_id)) {
ritm.work_notes = 'Synchronized Removal: Attachment "' + current.file_name + '" was deleted and removed from all related Catalog Tasks.';
ritm.update();
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
13m ago
you must be having some custom solution to copy file to SCTASK when file is added to RITM
So similarly you need to have custom solution for deleting as well
BR: Before Delete on sys_attachment
Condition: current.table_name == 'sc_req_item'
Script:
(function executeRule(current, previous) {
var ritmId = current.getValue('table_sys_id');
var fileName = current.getValue('file_name');
var gsa = new GlideSysAttachment();
var affectedTasks = [];
var task = new GlideRecord('sc_task');
task.addQuery('request_item', ritmId);
task.query();
while (task.next()) {
var att = new GlideRecord('sys_attachment');
att.addQuery('table_name', 'sc_task');
att.addQuery('table_sys_id', task.getUniqueValue());
att.addQuery('file_name', fileName);
att.query();
var removed = false;
while (att.next()) {
gsa.deleteAttachment(att.getUniqueValue());
removed = true;
}
if (removed) {
task.work_notes = 'Attachment "' + fileName + '" removed automatically after deletion from parent RITM.';
task.update();
affectedTasks.push(task.getValue('number'));
}
}
var ritm = new GlideRecord('sc_req_item');
if (ritm.get(ritmId)) {
ritm.work_notes = affectedTasks.length ?
'Attachment "' + fileName + '" deleted; synchronized removal completed on tasks: ' + affectedTasks.join(', ') :
'Attachment "' + fileName + '" deleted; no matching task attachments found.';
ritm.update();
}
})(current, previous);
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12m ago - last edited 11m ago
Hey @Ballela Siva Te
Business Rule
Table
sys_attachment
When
After Delete
Condition
current.table_name == 'sc_req_item'
Script
(function executeRule(current, previous) {
try {
// Execute only for RITM attachments
if (current.table_name != 'sc_req_item')
return;
var ritmSysId = current.table_sys_id;
// Update RITM work notes
var ritmGR = new GlideRecord('sc_req_item');
if (ritmGR.get(ritmSysId)) {
ritmGR.work_notes =
'Attachment "' + current.file_name +
'" was deleted from Service Portal and synchronized with related catalog tasks.';
ritmGR.update();
}
// Find related SCTASKs
var taskGR = new GlideRecord('sc_task');
taskGR.addQuery('request_item', ritmSysId);
taskGR.query();
while (taskGR.next()) {
// Find matching attachments on SCTASK
var attachGR = new GlideRecord('sys_attachment');
attachGR.addQuery('table_name', 'sc_task');
attachGR.addQuery('table_sys_id', taskGR.sys_id);
attachGR.addQuery('file_name', current.file_name);
attachGR.addQuery('size_bytes', current.size_bytes);
attachGR.query();
while (attachGR.next()) {
var deletedFile = attachGR.file_name.toString();
// Delete attachment from SCTASK
new GlideSysAttachment().deleteAttachment(attachGR.sys_id);
// Add work notes on SCTASK
taskGR.work_notes =
'Attachment "' + deletedFile +
'" was automatically removed because it was deleted from the parent RITM.';
taskGR.update();
}
}
} catch (ex) {
gs.error('Attachment Synchronization Error: ' + ex);
}
})(current, previous);****************************************************************************
If this response helps, please mark it as Accept as Solution and Helpful.
Doing so helps others in the community and encourages me to keep contributing.
Regards
Vaishali Singh
Servicenow Developer
Linkedin - https://www.linkedin.com/in/vaishali-singh-2273361bb
