Have 3rd party integration -How to send multiple Attachments from one Snow to Another Snow

Kishore Thippal
Tera Expert

Have a integration from SC task to RITM one ServiceNow to Another ServiceNow need to send multiple attachments i have written a script using attachments API /api/now/attachment/file  but im able to send only one attachment , here when I'm add the 1 or more in SC TASK i will take only one attachment please can anyone help with me

 

 

var fileName = [];
var ContentType = [];
var Tablename = '';
var TableSysID = '';
var attid = [];
var att = new GlideRecord('sys_attachment');
att.addEncodedQuery('sys_created_by!=ebonding^table_name=sc_task^sys_created_onRELATIVEGT@minute@ago@1');

att.addQuery('table_name', current.getTableName());
att.addQuery('table_sys_id', current.sys_id);
att.addQuery('sys_updated_by', '!=', 'ebonding');
att.addQuery('sys_created_by', '!=', 'ebonding');
// att.orderBy('sys_updated_on');
//att.orderByDesc('sys_updated_on');
att.query();


while (att.next()) {

Tablename = att.table_name;
TableSysID = att.table_sys_id;
ContentType = att.content_type;
fileName = att.file_name;
gs.log('---ATTACHMENT:' + Tablename + '' + TableSysID);

}


try {
var r = new sn_ws.RESTMessageV2(' Attachment', 'Create Attachment');

//r.setStringParameterNoEscape('table_name', 'sc_req_item');
r.setStringParameterNoEscape('table_sys_id', current.request_item.u_ritm_sys_id);
r.setStringParameterNoEscape('file_name', fileName);
r.setStringParameterNoEscape('content_type', ContentType);

r.setRequestBodyFromAttachment(att.sys_id);
r.setRequestHeader("Accept", "application/json");

var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.log('---ATTACHMENT=======:' + fileName + ' ' + ContentType + ' ' + TableSysID + '' + Tablename );

gs.log('Attachments Count:' + att.getRowCount());

} catch (ex) {
var message = ex.message;
}

 

 

 

Thanks,

Kishore.

3 REPLIES 3

Claude DAmico
Kilo Sage

It looks like your try/catch is not within your while loop so I can see that only sending one attachment which would be the last attachment from the while loop.

Claude E. D'Amico, III - CSA

Kishore Thippal
Tera Expert

Hi Claude DAmic ,

Thanks for the reply , it will helpul for me😀

 

Mathieu Lepoutr
Mega Guru

hi Kishore

 

It looks like you're trying to send attachments from an SC task in one ServiceNow instance to an RITM in another ServiceNow instance using the attachments API. The variables fileName, ContentType, Tablename, and TableSysID will only store the last attachment’s details due to the placement of these lines inside the while loop, but before any action is taken on them and the REST call to send the attachment is also outside the while loop, meaning it will only execute once and only for the last attachment processed by the loop.

 

To address these issues, you can:

  1. Make the REST call within the while loop so that it sends all the attachments, one by one.
  2. Directly use the attachment properties inside the REST call instead of storing them in temporary variables.

 

Please have a look at Exalate, a decentralized integration solution that instantly jumped into my mind reading this. It will make this integration a lot easier.