How to attach an attachment to a record via a script?

Mike114
Kilo Contributor

The attachment is already in the sys_attachment table. In a script, how would I add an attachment to a specific Incident record?

I know I would have to query for a specific attachment in the sys_attachment table, but I don't know how to assign that specific sys_id of an attachment to be able to have it attached to an Incident if I use a script.

1 ACCEPTED SOLUTION

asifnoor
Kilo Patron

Hello Mike,

You need to update table_sys_id in the sys_attachment table of your attachment with the sys_id of your incident. Then the attachment will be linked to your incident automatically. Here is the sample script. 

update the sys_ids with your actual sysids and check.

var gr = new GlideRecord("sys_attachment");
if(gr.get("ATTACHMENT_SYSID")) {
  gr.table_sys_id="INCIDENT_SYSID";
  gr.table_name="incident";
  gr.update();
}

Mark the comment as a correct answer and helpful if it has answered your question.

 

View solution in original post

7 REPLIES 7

The record producer has 'current.setAbortAction(true);' so it doesn't get to retain the attachment that a user picks, in which case a script is used to set the fields in a record, and I would also like to attach an attachment that a user picks while attempting to submit a record. Because the attachment gets placed into the sys_attachment table anyways, I figured I would grab it from there. 

I tried GlideSysAttachment.copy(sourcetable,sys_id,destinationtable,sys_id) but it didnt work for me. I will keep trying. 

If the attachment is on record producer, it should get copied to the target record by itself, why is that not happening.

-Anurag

-Anurag

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Mike,

Can you explain your business use-case/requirement around this?

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader