Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Attachment Move Integration

Mark Wood
Tera Contributor

Hello Experts,

I am trying to move an attachment from ServiceNow to Aha.

The Aha application requires the request body to be in the following format:

 

 

------------XnJLe9ZIbbGUYtzPQJ16u1
content-disposition: form-data; name="attachment[data]"; filename="sample_plain_text.txt"
content-type: text/plain
content-length: 27

[uploaded data]
------------XnJLe9ZIbbGUYtzPQJ16u1--

 

 

I have the following background script, but it is not working and I am receiving a "bad request" error. Could you please guide me on this?

Thank you.

 

Background script;

 

 

// Fetch the attachment from ServiceNow
var attachmentSysId = '53c4b8bc83fa4210ae8dc4a6feaad3ab';
var attachmentGr = new GlideRecord('sys_attachment');
if (attachmentGr.get(attachmentSysId)) {
    var attachment = new GlideSysAttachment();
    var attachmentStream = attachment.getBytes(attachmentGr);
    var base64Content = GlideStringUtil.base64Encode(attachmentStream);
gs.print(base64Content);
    // Prepare the payload for Aha!
    var payload = {
        "attachment": {
            "file_name": attachmentGr.file_name,
            "content": base64Content
        }
    };
 
    // Send the attachment to Aha!
 
    var request = new sn_ws.RESTMessageV2('Aha Integrations', 'Add Attchment on Comments');
 
    request.setRequestBody(JSON.stringify(payload));
 
    var response = request.execute();
    var responseBody = response.getBody();
    gs.info('Aha! response: ' + responseBody);
} else {
    gs.error('Attachment not found');
}
 

 

Aha Docs Link for reference:

https://www.aha.io/api/resources/attachments/create_an_attachment_on_a_comment

 

1 REPLY 1

Kris Moncada
Kilo Sage

Since it's a bad request, it may help to define the request headers like in the following example:

 

var sm = new sn_ws.RESTMessageV2("<REST_message_record>","get"); //Might throw exception if message doesn't exist or not visible due to scope.
sm.setRequestHeader("Accept","Application/json");