- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
To copy attachments from an SCTASK to a RITM, I used BusinessRule#1, which I found in a ServiceNow Community post
The problem is that each time an attachment is added to the SCTASK, all of its attachments are copied to the RITM, even those already there, which causes duplicates.
To solve this, I have BusinessRule#2 that automatically deletes an existing attachment on a RITM if a new, identical attachment (same name, type, size) is added, to avoid duplicates.
BusinessRule#1 (after - insert)
Condition : current.table_name == 'sc_task'
Script :
(function executeRule(current, previous /*null when async*/ ) {
var taskRec = new GlideRecord('sc_task');
taskRec.addQuery('sys_id', current.table_sys_id);
taskRec.query();
if (taskRec.next()) {
var ritmRec = new GlideRecord('sc_req_item');
ritmRec.addQuery('sys_id', taskRec.request_item);
ritmRec.query();
if (ritmRec.next()) {
GlideSysAttachment.copy('sc_task', current.table_sys_id, 'sc_req_item', ritmRec.sys_id);
}
}
})(current, previous);
BusinessRule#2 (before - insert, update)
Condition : current.table_name == 'sc_req_item'
Script :
(function executeRule(current, previous /*null when async*/ ) {
var attach = new GlideRecord('sys_attachment');
attach.addQuery('table_sys_id', current.table_sys_id);
attach.addQuery('table_name', current.table_name);
attach.addQuery('file_name', current.file_name);
attach.addQuery('content_type', current.content_type);
attach.addQuery('size_bytes', current.size_bytes);
attach.setLimit(1);
attach.query();
if (attach.next()) {
attach.deleteRecord();
}
})(current, previous);
This technique works well, but the following points bother me:
- With each new addition to the SCTASK, all the task's attachments are still copied to the RITM before the duplicates are deleted.
- Deletion is based solely on name, type, and size, so two different files that are identical based on these criteria risk being deleted by mistake.
- The two Business Rules execute one after the other (after insert + before insert), which can cause temporary duplicates, unexpected deletions, or an unpredictable execution order.
I'd like to copy only the newly added attachment to avoid any duplicates from the start. Do you know of a way to do this?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
This is way easier to do in Flow Designer. There are all kinds of Flow Actions for managing attachments now.
Plus you can isolate it to a single attachment via conditions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
Hi @Ankur Bawiskar !
Here's my new post on copying SCTASK Attachments.
Thanks in advance for your help
Regards,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
This is way easier to do in Flow Designer. There are all kinds of Flow Actions for managing attachments now.
Plus you can isolate it to a single attachment via conditions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
This way is great ! Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
You're welcome. One of the many reasons I'm "Flow First" on business logic.
Also, I know you didn't ask, but if you're using Flow Designer for attachment management, you can also zip/unzip the files.
https://www.youtube.com/watch?v=de1LnEPAVU0