Adding an attachment to a record via script

JJG
Kilo Guru

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();
}
3 REPLIES 3

chrisperry
Giga Sage

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

If this answer is helpful please mark correct and helpful!

Regards,
Chris Perry

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

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

If this answer is helpful please mark correct and helpful!

Regards,
Chris Perry