Sending attachment from ServiceNow to Jira using BR and REST message

Shalika
Tera Expert

I want to send attachment from ServiceNow to Jira. For that I have created REST message and BR. I don't know what is wrong with my BR. I am not able to send attachment

(function executeRule(current, previous /*null when async*/ ) {
  var included_tables = ["incident"];
var debug = true;

var arrayUtil = new ArrayUtil();
var requestBody;
var responseBody;
var status;
var sm;

    if (typeof GlideStringUtil != 'undefined')
        var StringUtil = GlideStringUtil;
    else
        var StringUtil = Packages.com.glide.util.StringUtil;
gs.log("initialize");

    var gr = new GlideRecord(current.table_name);
    gr.addQuery('sys_id', current.table_sys_id);
    gr.query();

    if (gr.next()) {
        if (arrayUtil.contains(included_tables,current.table_name)) {
            
            try{
                
        sm = new sn_ws.RESTMessageV2('Neste Siili JIRA', 'Add attachment');
       
                var attachmentsJson = '';
                attachmentsJson += ' { "content_type":"' + current.content_type + '",';
                attachmentsJson += '"file_name":"' + JSUtil.escapeText(current.file_name) + '",';
                attachmentsJson += '"size_bytes":"' + current.size_bytes + '",';
                attachmentsJson += '"sys_id":"' + current.sys_id + '"';
                attachmentsJson += '}';

                sm.setStringParameterNoEscape('attachments', attachmentsJson);

                response = sm.execute();
                status = response.getStatusCode();

            if (debug){
                    gs.log("Payload sent jira: " + sm.getRequestBody());
                    gs.log("Response jira: " + response.getBody());
                    gs.log("Status jira : " + status);
            }
            } catch(ex) {
                responseBody = ex.getMessage();
                status = '500';
            } finally {
                requestBody = sm ? sm.getRequestBody():null;
            }
        }
    }

})(current, previous);

1 ACCEPTED SOLUTION

Hi,

I am sharing a sample code and you can enhance it further

based on BR is on which table sys_attachment or incident enhance the code

var arr = [];

var gsu = (typeof GlideStringUtil != 'undefined') ? (GlideStringUtil) : (Packages.com.glide.util.StringUtil); //few versions support the first one, other supports second
var gsa = (typeof GlideSysAttachment != 'undefined') ? (new GlideSysAttachment()) : (new Packages.com.glide.ui.SysAttachment());

var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_sys_id", incidentSysId);
gr.query();
while (gr.next()) {

	var attachmentData = gsa.getBytes(gr);
	var attachment = String(Packages.java.lang.String(attachmentData));

	var encData = GlideStringUtil.base64Encode(attachmentData);

	var obj = {};
	obj["content_type"] = gr.getValue('content_type');
	obj["file_name"] = JSUtil.escapeText(gr.getValue('file_name'));
	obj["size_bytes"] = gr.getValue("size_bytes ");
	obj["sys_id"] = gr.getUniqueValue();
	obj["Content-Transfer-Encoding"] = "base64" +  encData.toString();

	arr.push(obj);

}

var finalObj = {};
finalObj["attachment"] = arr;

var sm = new sn_ws.RESTMessageV2('Neste Siili JIRA', 'Add attachment');

var response = sm.execute();
var status = response.getStatusCode();

gs.info("Payload sent jira: " + sm.getRequestBody());
gs.info("Response jira: " + response.getBody());
gs.info("Status jira : " + status);

Regards
Ankur

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

View solution in original post

24 REPLIES 24

In REST message, I am sending the content in payload as

{
      "attachments": [{
          "content_type": "image/png",
          "file_name": "Screenshot (153).png",
                "size_bytes ": "137246 ",
          "sys_id": "f576b5211b091950f7b82fcde54bcbae",
"Content-Transfer-Encoding": "base64${base64EncodedData}"
      }]
  }

 

I am getting status code as 200, but no attachment being added

Hi,

http status looks fine 200

But did you check with JIRA team whether they received the file or not

Did they share the sample json request body on how the file content should look like

Can you share your complete script and how your rest message is configured?

Regards
Ankur

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

In the REST message, I am using the POST method with endpoint as - https://**********/rest/api/3/issue/SSP-8430/comment where SSP-8430 is the issue ID.

The payload content and HTTP headers I have mentioned in the above thread.

In my logs, I can see the attachment is going because I am receiving 200 status code. At the Jira end, the Jira client people cannot check that they have received the attachment or not. 

They have not shared the sample JSON body content on how the file should look like, I got the requirement to attach the attachments from ServiceNow incident table to Jira issue.

Please help

Hi,

Did they not share how the sample JSON request body should look like?

Unless that is shared how can you be sure that the request body you are sending is what they are expecting?

Regards
Ankur

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

{
      "attachments": [{
          "content_type": "image/png",
          "file_name": "Screenshot (153).png",
           "size_bytes ": "137246 ",
          
      }]
  }

This is the sample JSON request body