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

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.

 

Mike114
Kilo Contributor

That's perfect. This is exactly what I need. Thank you.

Regarding the following line:  

if(gr.get("ATTACHMENT_SYSID")) {

is ATTACHMENT_SYSID the sys_id of the sys_attachment table OR the sys_id of the attachment/file?

-- If it is the sys_id of the attachment/file, what if that object is random or unknown?

-- It would make more sense if it was the sys_id of the table, since the file could be different for each task created.

-- I am possibly WAY overthinking this too. LOL

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

Looks like you are trying to copy an attachment?

If not then how did the document get to attachment table? What record is it currently linked to?

-Anurag

-Anurag