Inserting Attachments in ServiceNow from JIRA without MID-Server using REST

Rohit Kumar
Giga Expert

We were successfully able to insert attachments in ServiceNow from JIRA and hence below code can be used for other third party tools.

var targetInstanceURL =   "https://jira.******.com/secure/attachment/"+attachment ID+"/";

var targetUserID = "..";

var targetUserPassword = "...";

var sa = new GlideSysAttachment();

var StringUtil = GlideStringUtil();

var answer = "";

var attachmentMsg = {};

sendAttachments();

function sendAttachments() {

      var answer = [0, 0]; //successful attachments, failed attachments

                  //gs.log('Found a attachment##');

                      var attachmentMessage = new sn_ws.RESTMessageV2();

                      attachmentMessage.setHttpMethod("get");

                      attachmentMessage.setBasicAuth(targetUserID, targetUserPassword);

                      attachmentMessage.setEndpoint(targetInstanceURL);

                      attachmentMessage.saveResponseBodyAsAttachment("incident", current.sys_id, fileName);   // Attachment API documentation for this function

                      var response = attachmentMessage.execute();  

                    // var responseBody = response.getBody();

                      var httpStatus = response.getStatusCode();

                      if (httpStatus.toString() == "201") {

                              answer[0] += 1;

                      } else {

                              answer[1] += 1;

                      }        

      return answer;

}

I am still searching for ways to insert files in JIRA from SN without MID-Server.  

Do let me know in case of any questions !!!!

Thanks

Rohit

15 REPLIES 15

anupamg
Tera Guru

Hi tmackay

We are also building the custom scripts to send attachments to Jira from SN without using MID server. So far we are successful in sending .txt and .csv file but not the the word or excel file docs. .JPG and .PDF files we are able to send but not able to open at JIRA. 

We tried downloading your file  jira_script_include_b64.txt  but could not do so, probably because it is an old attachment.

Any help is greatly appreciated.

Thanks

tmackay
Giga Contributor

Hi anupamg,

Strange, I can still download the attachment. That aside, I wouldn't recommend that approach any more. What I've settled on is creating a Scripted REST resource which uses response.getStreamWriter() to construct the request body in suitable binary format for JIRA and the built-in RESTMessageV2.saveResponseBodyAsAttachment to save this as an attachment to the original attachment, then we pass it to Jira using setRequestBodyFromAttachment in the final RESTMessageV2. No need to stuff around with base64 intermediates - which was a bit of a hack to begin with. All the necessary code for the new approach is in this thread.

Regards,

Troy.

anupamg
Tera Guru

Thanks tmackay for the suggestions. We are able to resolve our issues with Jira & SN attachments..

Regards

Vanjuli
Giga Contributor

Hi anupamg,

How were you able to resolve same as I am facing same issues. Whenever I upload the attachment as PDF (via script in Virtual Agent) to service now table record. I am able to see the attachment n PDF format but can't view it after download as it fails to load. Any hints as to how you resolved similar issue would be helpful for me. Thanks

pooja123
Tera Contributor

Could you please help where can I use this code