VaranAwesomenow
Mega Sage
  1. Create a scripted REST API in ServiceNow with url parameter to hold operation and issue ID.
  2. https://dev63486.service-now.com/api/125590/jiraintegration/{operation}/{id}
  3. https://dev63486.service-now.com/api/125590/jiraintegration/JiraIssueCreation/${issue.id}
  4. Create a webhook in Jira, select trigger as insert on issue, use scripted REST API endpoint.
  5. Create a Outbound REST Message to get Issue details from Jira.
  6. https://xxxxxx.atlassian.net/rest/api/3/issue/${issueID}
  7. Create incident in ServiceNow based on issue details.

Note : Scripted REST API has authentication set to not required.

In order to allow ServiceNow to access Jira API API token has to be setup on Jira side.

 Jira API documentation : The Jira Cloud platform REST API (atlassian.com)

Jira API token documentation : Manage API tokens for your Atlassian account | Atlassian Support

Scripted REST API code

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

// implement resource here
var sLog = '';
var pathParams = request.pathParams;
var operation = pathParams.operation; //'myApp_table'
var issueId = pathParams.id; //'1234'

sLog += "\n operation=" + operation;
sLog += " \n id=" + issueId;
var issueDetails = '';
issueDetails = getIssueDetails(issueId);
//sLog += '\n IssueDetails=' + issueDetails;
var issueJson = JSON.parse(issueDetails);
var issueSummary = issueJson.fields.summary;
//var issueDesc = issueJson.fields.description;
var issueDesc = issueJson.fields.description.content[0].content[0].text;
//var issueDescString = JSON.stringify(issueDesc);
sLog += "\n Issue id = " + issueJson.id;
sLog += "\n Issue description = " + issueDesc;
sLog += "\n issueSummary = " + issueSummary;

gs.log(sLog, 'jiraint');
//Get Issue details from Jira and create an incident in ServiceNow
if (operation.indexOf('JiraIssueCreation') > -1) {
createIncident(issueId, issueSummary, issueDesc);
}

function createIncident(issueId, issueSummary, issueDesc) {
var grInc = new GlideRecord('incident');
grInc.initialize();
grInc.short_description = issueSummary;
grInc.description = issueDesc;
grInc.correlation_id = issueId;
grInc.correlation_display = 'jira';
grInc.insert();
}

function getIssueDetails(issueID) {
try {
var r = new sn_ws.RESTMessageV2('JiraIntegrationEndpoint', 'Get Issue Details');
// r.setStringParameterNoEscape('issueID', 'SE-23');
r.setStringParameterNoEscape('issueID', issueID);

//override authentication profile
//authentication type ='basic'/ 'oauth2'
//r.setAuthenticationProfile(authentication type, profile name);

//set a MID server name if one wants to run the message on MID
//r.setMIDServer('MY_MID_SERVER');

//if the message is configured to communicate through ECC queue, either
//by setting a MID server or calling executeAsync, one needs to set skip_sensor
//to true. Otherwise, one may get an intermittent error that the response body is null
//r.setEccParameter('skip_sensor', true);

var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
} catch (ex) {
var message = ex.message;
}
return responseBody;
//return JSON.stringify(request.body.data) ; //gs.log(JSON.stringify(request.body.data));
}


})(request, response);

Youtube link : https://youtu.be/bZT1LYSIomI

Comments
Sunny SN
ServiceNow Employee
ServiceNow Employee

This is good !... but would highly recommend taking a look at the Jira spoke and integrationhub. Makes life much much easier - no more writing code.

 
Nikesh5
Kilo Contributor

Hello Anil,

Thanks for the Article !!

 

I have a question here about authentication. 

 

Note: Scripted REST API has authentication set to not required.  ( Copied from your post)---  Does this mean anyone can access this rest API if they know the URL right ?, can't we make it safe by using authentication through Jira webhook?

VaranAwesomenow
Mega Sage

Dear Nikesh, Based on my understanding Jira webhook doesnt support authentication. So I have setup a workaround for this by sending a key via webhook as url parameter and that is validated in the script, if the key is incorrect I send a 4XX response.

 

jcordova
Mega Explorer

Do you have the code that you used for this?  I would be interested in seeing how you set that up. 

 

Thanks!

me2
Giga Contributor

please I need more info on the Jira spoke.

 

krishanu
Tera Contributor

Hello Anil,

Could you please help me to send attachment to JIRA using REST?

wanwan
Tera Contributor

Is there anything that has been implemented?

I can't connect jira and servicenow here

Marwan_
Mega Expert

Hi @jcordova @me2 @krishanu @wanwan and everyone else off course!

 

Connecting ServiceNow and Jira using these methods can be very time consuming and a hassle to setup.

A very good alternative is to use a specialised solution that provides you all the flexibility you need without all the hassle of trying to get it to work on your own.

 

A great solution for this is Exalate. As it is a fully decentralised solution, it allows you to connect ServiceNow to Jira and many other ITSM solutions. While allowing both systems to keep their workflows and metrics in place. It provides ways for you to decide what information you want to send from one system to another (including attachments) and full control on how this information is being received and where it gets assigned to/displayed as well.

 

If you have any questions please let me know.

 

Kind regards,

Marwan

sangitakumari
Tera Contributor

Hi @VaranAwesomenow ,

I have done same as you shown in above. But ticket is not created from Jira to ServiceNow. could you help me on that. Ticket is not created from Jira to ServiceNow instance. Done the same step. Please check below screenshot.

sangitakumari_0-1705047433770.png

 

sangitakumari_1-1705047459999.png

 

sangitakumari_2-1705047486320.png

 

sangitakumari_3-1705047549093.png

 

sangitakumari_4-1705047584874.png

 

sangitakumari_5-1705047673804.png

 

sangitakumari_6-1705047695870.png

 

sangitakumari_7-1705047806648.png

 

 

//var sLog = '';
var pathParams = request.pathParams;
var operation = pathParams.operation; //'myApp_table'
var issueId = pathParams.id; //'1234'
var req = request.body.data;
var webhookevent = req.webhookEvent;

var sLog = '';

sLog += "\n operation=" + operation;
sLog += " \n id=" + issueId;
var issueDetails = '';
issueDetails = getIssueDetails(issueId);
//sLog += '\n IssueDetails=' + issueDetails;
var issueJson = JSON.parse(issueDetails);
var issueSummary = issueJson.fields.summary;
//var issueDesc = issueJson.fields.description;
var issueDesc = issueJson.fields.description.content[0].content[0].text;
//var issueDescString = JSON.stringify(issueDesc);
sLog += "\n Issue id = " + issueJson.id;
sLog += "\n Issue description = " + issueDesc;
sLog += "\n issueSummary = " + issueSummary;

gs.log(sLog, 'jiraint');
//Get Issue details from Jira and create an incident in ServiceNow
if (operation.indexOf('JiraIssueCreation') > -1) {
createIncident(issueId, issueSummary, issueDesc);
}

function createIncident(issueId, issueSummary, issueDesc) {
var grInc = new GlideRecord('incident');
grInc.initialize();
grInc.short_description = issueSummary;
grInc.description = issueDesc;
grInc.correlation_id = issueId;
grInc.correlation_display = 'jira';
grInc.insert();
}

function getIssueDetails(issueID) {
try {
var r = new sn_ws.RESTMessageV2('testing jira external', 'Get Issue details');
// r.setStringParameterNoEscape('issueID', 'SE-23');
r.setStringParameterNoEscape('issueID', issueId);


var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
} catch (ex) {
var message = ex.message;
}
return responseBody;
//return JSON.stringify(request.body.data) ; //gs.log(JSON.stringify(request.body.data));
}

 

SasiKiranG
Tera Contributor

Hello,

 

I have one doubt here,

 

How "JiraIssueCreation" comes in below end point?JIRA end point.png

  1. https://dev63486.service-now.com/api/125590/jiraintegration/JiraIssueCreation/${issue.id}
VaranAwesomenow
Mega Sage

@SasiKiranG  I believe its a url parameter to identify the operation so that scripted REST API can perform the next steps, it available in scripted REST API code provided in the post.

Version history
Last update:
‎03-11-2021 01:27 AM
Updated by: