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-11-2022 01:44 AM
Hello,
Please help me with the script.
var queryString = "table_nameINsc_task^table_sys_idIN" + current.sys_id;
var taskGR = new GlideRecord("sc_task");
taskGR.addQuery("request_item", current.sys_id);
taskGR.query();
while (taskGR.next()) {
queryString += "," + taskGR.sys_id.toString();
}
current.addEncodedQuery(queryString);
This is not working.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2022 04:16 AM
You can use the below line of code
GlideSysAttachment.copy('sourcetable','sys_id','destinationtable','sys_id');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-11-2022 03:30 AM
Did you try this ???
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2022 04:20 AM
Hi,
I would suggest to avoid copying the files as it would increase the size of sys_attachment table
Instead you can refer this article and show the attachments in the related list.
"Related Attachments" Related List
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2022 04:54 AM
Hope you are doing good.
Did my reply answer your question?
If my response helped please close the thread by marking appropriate response as correct so that it benefits future readers.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader