Attachment Integration

Mayur Patil2
Tera Expert

Hello Everyone,

I am trying to integrate attachments from the incident table from the source instance to the target instance using REST Message. 

Basic Condition 

1) use REST Message with the GET method to retrieve the data also with Table API

2) use Schedule Job

 

I have written one code in Schedule Job, but I am only getting the file. When I am trying to open the file it is showing empty.

Below is the code:-

try {

 

var encodedFile;
var fileName;
var contentType;

 

var r = new sn_ws.RESTMessageV2('Attachment', 'attachment get');
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.log("response body " + responseBody + "httpStatus" + httpStatus);

 

var parsedData = JSON.parse(responseBody);
for (i = 0; i < parsedData.result.length; i++) {
var att = new GlideRecord('sys_attachment');
if (att.next()) {
var gsa = GLideSysAttachmentInputStream(att.sys_id.toString());
var baos = new Packages.java.io.ByteArrayOutputStream();
gsa.writeTo(baos, 0, 0);
baos.close();
att.encodedFile = parsedData.result[i].GlideStringUtil.base64Encode(baos.toByteArray());
att.file_name = parsedData.result[i].file_name;
att.content_type = parsedData.result[i].content_type;
}
att.addQuery("table_name", parsedData.result[i].getTableName());
att.addQuery("table_sys_id", parsedData.result[i].sys_id);
att.addQuery("file_name",parsedData.result[i].file_name);
att.addQuery("content_type",parsedData.result[i].content_type);

att.addQuery();
att.query();
att.initialize();
att.file_name = parsedData.result[i].file_name;
att.content_type = parsedData.result[i].content_type;

att.insert();

}

 

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

 

Please help me to get the data in file. 

 

Thanks and Regards

Mayur Patil

 

 

3 REPLIES 3

Anish Reghu
Kilo Sage
Kilo Sage

@Mayur Patil2 

It looks like you are trying to parse the response body as a JSON object and then loop through the "result" field to access the attachment information, but the "result" field is not defined in the response. Instead, you should be able to access the attachment information directly from the "parsedData" object.

Try changing the loop to:

for (i = 0; i < parsedData.length; i++) {
var att = new GlideRecord('sys_attachment');
if (att.next()) {
var gsa = GLideSysAttachmentInputStream(parsedData[i].sys_id.toString());
var baos = new Packages.java.io.ByteArrayOutputStream();
gsa.writeTo(baos, 0, 0);
baos.close();
att.encodedFile = GlideStringUtil.base64Encode(baos.toByteArray());
att.file_name = parsedData[i].file_name;
att.content_type = parsedData[i].content_type;
}
att.addQuery("table_name", parsedData[i].table_name);
att.addQuery("table_sys_id", parsedData[i].table_sys_id);
att.addQuery("file_name", parsedData[i].file_name);
att.addQuery("content_type", parsedData[i].content_type);
att.query();
att.initialize();
att.file_name = parsedData[i].file_name;
att.content_type = parsedData[i].content_type;
att.insert();
}

 

This should allow you to access the attachment information directly from the "parsedData" object.

You may also want to consider adding some error handling to your code, in case there are any issues with the REST message execution or the parsing of the response body as a JSON object.

Kindly mark the response as Correct or Helpful.

Cheers,

Anish

Hi Anish,

I tried your code but it give me the same result. Can you provide me with any documents or so where I can get the solution for this?

 

Thanks

Mayur

Ankur Bawiskar
Tera Patron
Tera Patron

@Mayur Patil2 

you want to transfer attachment from one instance to another between records

check this and it has solution

https://www.servicenow.com/community/developer-forum/transfer-attachment-from-one-to-another-instanc... 

 

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