Unable to add an attachment using background script

ghouse
Tera Contributor

Hi service now proffessionals,

                         Iam trying to add an attachment to an closed incident via background scripts but iam unable to add , below is the script im using to add . can you please suggest if this script is right or any modifications need to be made. 

var gr = new GlideRecord('sys_attachment');

gr.addEncodedQuery('GOTOtable_sys_idLIKE46afe2b41b92891036340ed6624bcb11');

gr.query();

if(gr.next()){

gr.table_name = 'incident';

gr.table_sys_id = '7b13faae1b8e4950a9ea21b3b24bcba2';

gr.update();

}

I also tried with an alternative script change but that didnt work 

var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_name','sc_req_item');
gr.orderBy('table_sys_id');
gr.query();
var inc = "7b13faae1b8e4950a9ea21b3b24bcba2";
while(gr.next()){
inc = gr.table_sys_id;
//gr.table_name = 'sc_req_item';
gr.table_sys_id = '7b13faae1b8e4950a9ea21b3b24bcba2';
//inc = gr.table_sys_id;
//grCI.setWorkflow(false); //Do not run business rules
//grCI.autoSysFields(false); //Do not update system fields
gs.addInfomessage('it will work now');
gr.update;
}

Can you please help me in providing best solution in order to fix it. Thank you.

Regards,

Ghouse sharief

2 REPLIES 2

sachin_namjoshi
Kilo Patron
Kilo Patron

You need to use GlideSysAttachment.copy() API to copy attachments.

 

https://developer.servicenow.com/dev.do#!/reference/api/rome/server/no-namespace/c_GlideSysAttachmentScopedAPI

 

Regards,

Sachin

hi sachin,

                Thanks for the reply , i modified the script as per my need and updated the script . the script is running and the info is generated .However , the attachment is not added to the incident record . could you please review the error i have made or suggest how good can i modify the script.

var attachment = new GlideSysAttachment();
var incidentSysID = '46afe2b41b92891036340ed6624bcb11';
//var table_sys_id= '7b13faae1b8e4950a9ea21b3b24bcba2';
var incGR = new GlideRecord('incident');
incGR.get(incidentSysID);

var copiedAttachments = attachment.copy('sc_req_item', incidentSysID, 'incident', incGR.getValue('7b13faae1b8e4950a9ea21b3b24bcba2'));
gs.info('Copied attachments: ' + copiedAttachments);

The output iam getting is as shown in the screenshot :

find_real_file.png

find_real_file.png