How to copy attachment from one record to other
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2022 03:41 AM
Hello,
I have a basic requirement of adding an attachment from sc_task to its RITM. How do I achieve it?
I have written a After Insert/Update BR on sc_task table with this script:
var gr = new GlideRecord("sc_req_item");
gr.addQuery("sys_id", current.request_item);
gr.query();
if (gr.next()) {
if (gr.cat_item.category.getDisplayValue() == 'Departmental Services') {
var attachment = new GlideSysAttachment();
var copiedAttachments = attachment.copy('sc_task', current.sys_id, 'sc_req_item', gr.sys_id);
}
}
This works only when the user add the attachment to sc_task and Saves the record. However I do not wish to save the record because that is unnecessary having the user to click.
So I wrote another After Insert/Update BR on sys_attachment table with thisscript:
var taskGR = new GlideRecord("sc_task");
taskGR.addQuery("sys_id", current.table_sys_id);
taskGR.query();
if (taskGR.next()) {
var ritmGR = new GlideRecord("sc_req_item");
ritmGR.addQuery("sys_id", taskGR.request_item);
ritmGR.query();
if (ritmGR.next()) {
if (ritmGR.cat_item.category.getDisplayValue() == 'Departmental Services') {
var attachment = new GlideSysAttachment();
var copiedAttachments = attachment.copy(current.table_name, current.table_sys_id, 'sc_req_item', ritmGR.sys_id);
}
}
And this is running in Infinite loop (I saw 500+ attachments got added to the RITM)
What is the best possible solution to my requirement?
Thank you.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2022 03:47 AM
Hi,
BR on Attachment table needs to be only on insert. No need of update.
Also, instead of copying attachment & duplicating why not use a relationship. Refer article for a check.
You can alter it as per your need.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2022 04:00 AM
hello jaspal,
can you help me
https://community.servicenow.com/community?id=community_question&sys_id=41613ced1b2cdd90a59033f2cd4bcbcb
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2022 04:11 AM
Something is seriously wrong.
I did the exact same way what you suggested:
With this, I am still getting to Infinite Loop.
i tried https://<INSTANCE>.service-now.com/cancel_my_transaction.do
But it not getting cancelled as well.
Where I must be getting wrong? Please help!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2022 01:10 AM