Adding an attachment to a record via script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-02-2022 11:38 AM
I have an attachment in the sys_attachment table which is a txt file containing the word "Test".
I am trying to duplicate and assign this attachment to a task record via onDisplay business rule.
It is duplicating and assigning the attachment to the task record, but when I open the new attachment, it is blank. The word "Test" is gone. What am I missing?
Here is the script I am using:
var sysID = current.sys_id;
var gr = new GlideRecord("sys_attachment");
if (gr.get("da6743901bdf4d100285520d0a4bcb5c")) { //This is the sys_id of the attachment
gr.table_sys_id = sysID;
gr.table_name = "sn_hr_core_task"; // This is the table where the task record is
gr.insert();
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-02-2022 11:43 AM
Hi there,
You should use the GlideSysAttachment API for your requirement as below:
GlideSysAttachment.copy('current_table_name', current.getUniqueValue(), 'target_record_table_name', 'target_record_sys_id');
If this answer is helpful please mark correct and helpful!
Regards,
Christopher Perry
Regards,
Chris Perry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-02-2022 12:31 PM
I tried this, but it is not working. Am I missing something?
var sysID = current.sys_id; //This is the sys_id of the task record in the sn_hr_core_task table
GlideSysAttachment.copy('sn_hr_core_task', sysID, 'sysauto_script', 'c33743901bdf4d100285520d0a4bcb93');
// the attachment I am trying to copy is in the sysauto_script table
// c33743901bdf4d100285520d0a4bcb93 is the sys_id of the sysauto_script record

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-02-2022 12:37 PM
Hmm the script looks good to me, but it may be a cross-scope access or ACL permissions issue. Are you seeing any warnings or errors in the System Logs?
You could also try re-writing the script as below:
var sysID = current.sys_id; //This is the sys_id of the task record in the sn_hr_core_task table
var attch = new GlideSysAttachment();
attch.copy('sn_hr_core_task', sysID, 'sysauto_script', 'c33743901bdf4d100285520d0a4bcb93');
// the attachment I am trying to copy is in the sysauto_script table
// c33743901bdf4d100285520d0a4bcb93 is the sys_id of the sysauto_script record
If this answer is helpful please mark correct and helpful!
Regards,
Christopher Perry
Regards,
Chris Perry