How do I get the attachments from Jira to ServiceNow using Rest API.

Rohit18
Tera Contributor

Dear All,

 

Requirement: How can i get the attachments populated on the Incidents when an issue is created in Jira?

Accomplished so far:

  • Webhook configured in Jira.
  • Scripted Rest API and Resource created in ServiceNow to fetch the payload details from Jira.
  • Issue/Comments creation/update in JIRA, creates a new/update existing incident (works perfectly fine).

Note: We are not using Jira Spoke for this integration.

 

7 REPLIES 7

Hi @Ratnakar7,

 

Thank you for your response. Your code works perfect and attachments are now being sent to servicenow. The issue is I am unable to open the any of the attachments on the ServiceNow side. Have tried the PDF, .text, .csv, etc.

On the attachments table, one thing which I noticed is the content type of the attachments is showing as undefined. I tried to update your code as below, by changing it to mimeType, it shows the applicable Content Type on the attachment table but still no luck:

var attachmentSysId = new GlideSysAttachment().write(jiraRecord, attachment.filename, attachment.mimeType, attachmentResponseBody);

 

Also, with this code, every time when I sent a new attachment, old attachments also gets passed, hence creating duplicates. I tried creating the following Business rule (before insert on the Incident table) to abort insert but still it is doing:

 

(function executeRule(current, previous /*null when async*/ ) {

// Add your code here

var attName = new GlideRecord('sys_attachment');
attName.addQuery('table_name', 'incident');
attName.addQuery('table_sys_id', current.table_sys_id);
attName.addQuery('file_name', current.file_name);
attName.query();
if(attName.next()){
gs.addInfoMessage('Duplicate file cannot be uploaded');
current.setAbortAction(true);
}


})(current, previous);

 

 

 

Hi @Rohit18 ,

 

You could try setting the Content-Type explicitly in your code by adding a line like this before writing the attachment:

attachmentData.setRequestHeader('Content-Type', attachment.contentType);

This should set the Content-Type of the attachment in ServiceNow correctly, which might solve the issue of not being able to open the attachments.

Regarding the issue of duplicates being created every time a new attachment is sent,One possible reason for this could be that the condition in the rule is not checking for the correct field values. You could try modifying the condition in the rule to check for the attachment name and incident number instead of just the incident number.

Here's an example:

(function executeRule(current, previous /*null when async*/ ) {

  var attName = new GlideRecord('sys_attachment');
  attName.addQuery('table_name', 'incident');
  attName.addQuery('table_sys_id', current.table_sys_id);
  attName.addQuery('file_name', current.file_name);
  attName.query();

  if(attName.next()){
    gs.addInfoMessage('Duplicate file cannot be uploaded');
    current.setAbortAction(true);
  }

})(current, previous);

 

Thanks,

Ratnakar

 

 

Hi @Ratnakar7 .

I did set up the same yesterday as well but it did not work. On the attachments table, for every incoming attachment the size bytes is 94. Below is the reference code where I did set the Content-Type:

 

for (var i = 0; i < requestBody.issue.fields.attachment.length; i++) {
var attachment = requestBody.issue.fields.attachment[i];
// download attachment from Jira
var attachmentData = new sn_ws.RESTMessageV2();
attachmentData.setRequestHeader('Content-Type', attachment.contentType);
attachmentData.setBasicAuth(gs.getProperty('jira.username'), gs.getProperty('jira.password'));
attachmentData.setHttpMethod('get');
attachmentData.setEndpoint('https://**********/rest/api/2/attachment/' + attachment.id);

gs.log("Attachment Response:" + attachment.id);
var attachmentResponse = attachmentData.execute();
gs.log("Attachment Response 1:" + attachmentResponse);
var attachmentResponseBody = attachmentResponse.getBody();
gs.log("Attachment Response Body:" + attachmentResponseBody);
var attachmentMimeType = attachment.mimeType;
gs.log("Attachment Response 2 : " + attachment);


// attach attachment to ServiceNow incident
attachmentData.setRequestHeader('Content-Type', attachment.contentType);
var attachmentSysId = new GlideSysAttachment().write(jiraRecord, attachment.filename, attachment.contentType, attachmentResponseBody);