RITM to SCTASK attachment copy – duplicates and email attachments corrupted
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Good day, everyone. I hope you're all doing well.
I am trying to copy attachments from RITM to SCTASK using a Business Rule on sys_attachment (after insert).
Flow:
- User uploads files in portal → RITM created
- User sends additional attachments via email
- SCTASK is created
Issue:
1. Attachments are getting duplicated in SCTASK
2. Email attachments are getting corrupted in SCTASK (but work fine in RITM)
Implementation on BR on sys_attachment (after insert) with condition tablename = sc_req_item
Script:-
What is the recommended way to copy RITM attachments to SCTASK without duplication and corruption (especially for email attachments).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi,
The issue is happening because the Business Rule is deleting and re-copying all attachments every time a new attachment is added. This leads to duplicates and also causes corruption for email attachments, as they may not be fully processed when the rule runs.
Recommended Approach
Instead of deleting and copying all attachments, you should copy only the newly added attachment and add a check to avoid duplicates.
Updated Script
if (current.table_name != 'sc_req_item')
return;
var ritmId = current.table_sys_id;
var grSCTask = new GlideRecord('sc_task');
grSCTask.addQuery('request_item', ritmId);
grSCTask.query();
while (grSCTask.next()) {
// Check if attachment already exists
var attCheck = new GlideRecord('sys_attachment');
attCheck.addQuery('table_sys_id', grSCTask.sys_id);
attCheck.addQuery('file_name', current.file_name);
attCheck.query();
if (!attCheck.hasNext()) {
var gsa = new GlideSysAttachment();
gsa.writeContentStream(
grSCTask,
current.file_name,
current.content_type,
gsa.getContentStream(current.sys_id)
);
}
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hello @ayushraj7012933
Thanks for your suggestion, it helped in resolving the duplicate attachment issue.
However, I am still facing one problem with email attachments.
Now:
- Duplicate files are no longer created in SCTASK
- Portal-uploaded attachments are copying correctly
- But attachments coming via email are not getting copied to SCTASK (they are present in RITM)
It seems the Business Rule is triggering before the email attachment is fully processed, so the content stream may not be available at that time.
Have you encountered this scenario before?
Would you recommend handling this using an event/script action or any delayed processing approach?
Thanks again for your help!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @RushikeshB24337,
Glad to hear the duplicate issue is resolved
Regarding email attachments, you’re correct — this is a known behavior. When attachments come via email, the Business Rule can trigger before the attachment content is fully written, which is why it doesn’t get copied.
Recommended Approach
Handle this asynchronously instead of copying immediately in the Business Rule.
Option 1: Use Event + Script Action (Best Practice)
In Business Rule:
Create Script Action on event ritm.attachment.copy :
var attachmentId = parm1;
var ritmId = parm2;
// Small delay to ensure attachment is ready
gs.sleep(2000);
var attGR = new GlideRecord('sys_attachment');
if (!attGR.get(attachmentId))
return;
var grSCTask = new GlideRecord('sc_task');
grSCTask.addQuery('request_item', ritmId);
grSCTask.query();
while (grSCTask.next()) {
var attCheck = new GlideRecord('sys_attachment');
attCheck.addQuery('table_sys_id', grSCTask.sys_id);
attCheck.addQuery('file_name', attGR.file_name);
attCheck.query();
if (!attCheck.hasNext()) {
var gsa = new GlideSysAttachment();
gsa.writeContentStream(
grSCTask,
attGR.file_name,
attGR.content_type,
gsa.getContentStream(attGR.sys_id)
);
}
}
})();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hello @ayushraj7012933
Thanks for your help — the Event + Script Action approach worked for duplicates
However, email attachments are still inconsistent, so I’m exploring alternative approaches as well.
Really appreciate your guidance!
