Sync Attachment between sc_request to sc_task and visa versa
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2025 08:37 PM
I have a requirement to sync attachment between sc_request and sc_task and vis versa
I tried the below script using business rule while it is working for sc_task to sc_request . I tried using similar logic to copy from sc_request to sc_task it is not working. Seeking our assistance.
sc_task to sc_request
Table:sys_attachment
when to run: after insert
condition: Table name is sc_task
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2025 09:27 PM
Hi, I can see the GlideSysAttachment syntax from the script is incorrect and we should give the records sys id not table sys id.
Replace the GlideSysAttachment.copy(current.table_name.toString(), current.table_sys_id.toString(), "sc_request", taskRec.request.toString()); line with
var attchment=new GlideSysAttachment();
attchment.copy("source_table_name", "source_table_record_sysId", "target_table_name", "target_table_record_sysId");
OR
new GlideSysAttachment().copy("source_table_name", "source_table_record_sysId", "target_table_name", "target_table_record_sysId");
Thanks, and Regards
Mahesh
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2025 09:58 PM
this will work to copy from sc_request to sc_task
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
// query to find sc_task record for this REQ sysId
var gr = new GlideRecord("sc_task");
gr.addQuery("request", current.table_sys_id);
gr.query();
if (gr.next()) {
var attachment = new GlideSysAttachment();
attachment.copy(current.table_name.toString(), current.table_sys_id.toString(), "sc_task", gr.sys_id.toString());
}
})(current, previous);
I will suggest not to copy attachments rather use related list feature and show the files in related list
This will help in keeping attachment table size as lower
TNT: "Related Attachments" Related List
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2025 03:59 AM
Thanks @Ankur Bawiskar , the script is working. But when I add an attachment via a portal/Native UI, it is duplicating the number of attachments with query continuously executing.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2025 04:02 AM
Glad to know that my script is working.
Please mark my response as correct and close the thread.
The discussion can continue on answered thread as well.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader