Sending Attachments via REST¶
Overview¶
This will allow a system to receive a base64 encoded string and set it to the attachment table
Staging Table¶
A table should be created that extends the Import Set Row table with the following added fields
Column label Type Max length attachment String 1,000,000 name String ticket number String 1,000
Create a Transform Map¶
Name Source table Target table Attachment Stage to Queue Attachment Staging Table ecc_queue
Field Maps¶
Source field Target field u_attachment payload u_name name u_ticket_number source [Script] agent [Script] topic
Agent Scripts¶
answer = (function transformEntry(source) { // Add your code here return "AttachmentCreator"; // return the value to be put into the target field })(source);
Topic Scripts¶
answer = (function transformEntry(source) { // Add your code here return "AttachmentCreator"; // return the value to be put into the target field })(source);
Sending the REST Message¶
The rest message should be sent with the fields below
{ "u_name":"Something.jpg:image/jpeg", "u_ticket_number":"incident:83bff1fddb2d220003ee793ebf961957", "u_attachment":"e490qugijelkabvrohatge4", }
Example Business Rule Script for Attachment¶
var target = new GlideRecord('sys_attachment'); target.addQuery('table_name', 'incident'); target.addQuery('table_sys_id', current.sys_id); target.query(); while(target.next()) { var sa = new GlideSysAttachment(); var binData = sa.getBytes(target); var base64Data = GlideStringUtil.base64Encode(binData); //Send Attachments var requestAttachment = new sn_ws.RESTMessageV2(); requestAttachment.setEndpoint('https://XXXXX.service-now.com/api/now/import/u_attachment_staging_table'); requestAttachment.setHttpMethod('POST'); requestAttachment.setBasicAuth(user,password); requestAttachment.setRequestHeader("Accept","application/json"); requestAttachment.setRequestHeader('Content-Type','application/json'); var requestAttachmentJSONBody = createRequestBody("u_name", target.file_name + ":" + target.content_type); requestAttachmentJSONBody += addToRequestBody("u_ticket_number", 'incident:' + sysid); requestAttachmentJSONBody += addToRequestBody("u_attachment", base64Data); requestAttachmentJSONBody += closeRequestBody(); requestAttachment.setRequestBody(requestAttachmentJSONBody); var responseAttachment = requestAttachment.execute(); }The createRequestBody() functions are just used to create a JSON Object with for example {u_name: “targetinfo”, u_ticket_number: “incident:39024903284”}