How do I get the attachments from Jira to ServiceNow using Rest API.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2023 11:56 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-16-2023 09:56 PM
Does anyone know how to achieve it. I am not able to find any thing related to it in the community.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2023 07:13 AM - edited 04-17-2023 07:13 AM
Hi
Please update your scripted Rest API and resource in ServiceNow to handle the attachments and then in your Scripted Rest API, extract the attachment details from the Jira payload.
For example:
var issue = requestBody.issue;
var attachments = issue.fields.attachment;
for (var i = 0; i < attachments.length; i++) {
var attachment = attachments[i];
var attachmentUrl = attachment.contentUrl;
var attachmentName = attachment.filename;
var attachmentMimeType = attachment.mimeType;
}
There is also a software integration tool called Exalate that can do this for you. It will handle unlimited amounts of attachments between Jira and ServiceNow.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2023 10:35 AM
Hello Mathieu,
Thank you for your reply. I have tried the following steps so far but still not able to get the attachments. I added your code as well but no result, hence sending you the code that has been created so far.
If you could please help in getting the attachments from Jira, that would be great.
Created Scripted Rest API and Resource has been attached for your reference. Below is the script that I have used in the resource.
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
// implement resource here
// authentication token verification
var queryParams = request.queryParams;
var token = queryParams.token;
var auth = "no";
if (token == gs.getProperty('jira.token')) {
auth = "yes";
}
if (auth == "yes") {
try {
gs.log("Jiralog " + JSON.stringify(request.body.data));
var body = JSON.stringify(request.body.data);
var parser = new JSONParser();
var parameterArr = parser.parse(body);
var requestBody = request.body.data;
// read the request body and map with incident table fields and create/update an issue record
gs.log('jiralog ' + requestBody.issue.fields.project.name);
var jiraRecord = new GlideRecord('incident');
jiraRecord.addQuery('correlation_id', requestBody.issue.key);
//jr.addQuery('correlation_display',requestBody.issue.fields.project.key);
jiraRecord.query();
if (!jiraRecord.next()) {
jiraRecord.initialize();
jiraRecord.correlation_display = requestBody.issue.fields.project.key;
jiraRecord.correlation_id = requestBody.issue.key;
jiraRecord.short_description = requestBody.issue.fields.summary;
jiraRecord.description = requestBody.issue.fields.description;
jiraRecord.insert();
gs.log('Jira Issue created: ' + jiraRecord.correlation_id);
} else {
jiraRecord.short_description = requestBody.issue.fields.summary;
jiraRecord.description = requestBody.issue.fields.description;
jiraRecord.comments = 'Commented by: ' + parameterArr.comment.author.displayName + '\nComment: ' + requestBody.comment.body;
jiraRecord.update();
gs.log('Jira Issue updated: ' + jiraRecord.correlation_id);
}
} catch (ex) {
var message = ex.message;
}
}
})(request, response);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2023 11:17 PM
Hi @Rohit18 ,
-> Add the following lines of code after the jiraRecord.initialize(); statement:
// loop through attachments
if (requestBody.issue.fields.attachment.length > 0) {
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.setBasicAuth(gs.getProperty('jira.username'), gs.getProperty('jira.password'));
attachmentData.setHttpMethod('get');
attachmentData.setEndpoint('https://your-jira-domain.com/rest/api/2/attachment/' + attachment.id);
var attachmentResponse = attachmentData.execute();
var attachmentResponseBody = attachmentResponse.getBody();
// attach attachment to ServiceNow incident
var attachmentSysId = new GlideSysAttachment().write(jiraRecord, attachment.filename, attachment.contentType, attachmentResponseBody);
gs.log('Attachment ' + attachment.filename + ' attached to incident ' + jiraRecord.number);
}
}
-> Replace your-jira-domain.com with your Jira instance's domain name in the attachmentData.setEndpoint() method.
-> Set the jira.username and jira.password properties with the credentials required to access the Jira REST API.
This code loops through each attachment in the Jira issue and downloads it using a REST message. It then attaches the downloaded attachment to the corresponding ServiceNow incident using the GlideSysAttachment().write() method. Finally, it logs a message indicating which attachment was attached to which incident.
If my response was helpful in resolving the issue, please consider accepting it as a solution by clicking on the ✅Accept solution button and giving it a thumbs up 👍. This will benefit others who may have a similar question in the future.
Thank you!
Ratnakar