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

I have shared all the related scripts.

Please debug from your side.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards
Ankur

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

@Shalika 

Hope you are doing good.

Did my reply answer your question?

If my response helped please close the thread by marking appropriate response as correct so that it benefits future readers.

Regards
Ankur

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

Hi ankur, Can you please tell in this way what should be the content in the http mehtod be? 

Hi Ankur Bawiskar,

I am trying to send an attachment from ServiceNow to Jira using BR and REST message.

The use case is "when a story is created in ServiceNow then a issue should be auto created in the Jira". When I followed the process of sending an attachment that you have mentioned above, I am getting the error response as 'Jira issue creation response : {"errorMessages":[],"errors":{"attachment":"string expected at index 0"}}' and no issue is creating at Jira side.

 

Can you help me out with this?

Mathieu Lepoutr
Mega Guru

Hi @Shalika 

 

I'm working as a pre-sales engineer with Exalate.

 

I can see the solution has already been published.

 

May I suggest you have a look at our decentralized solution please; it has the capability to syncronize all entity types between SNOW and Jira.

 

Exalate is easy and straightforward to set-up and you can control exactly which data you want to send and/or receive. 

If you would like to see a customized demo of the product, please book a slot.

Cheers