Advanced mode (scripting) allows you to customize app logic for building data that will be sent to apps via API. When adding a script to the fields in the Advanced Configuration section, ensure that it adheres to JavaScript (JS) syntax. Essentially, this script runs within Getint when specific events are triggered.
To access the Advanced Scripting tab and understand the available variables for your integration, follow these steps:
From your integration editor, click the More button
Then, click Advanced
The Advanced Configuration tab will be displayed, each checkbox enables a code row that can be used to run custom scripts that make the tool send specific requests before specific steps of the synchronization process.
Scripts applied to Before integration runs will trigger every time that an integration is going to run. Here we can provide some logic whenever an integration is syncing, as long as it complies with JavaScript (JS) syntax.
List of events you can define with the scripting feature
Before integration runs
Is run every time just before integration runs:
api.leftApp.fetch(url) - fetch data from endpoint from LEFT side app
api.leftApp.post(url, postData) - post data to LEFT side app
api.rightApp.fetch(url) - fetch data from endpoint from RIGHT side app
api.rightApp.post(url, postData) - post data to RIGHT side app
api.log(string) - write a log line to log file of the run
Before the item request object sent
Is run before CREATE or UPDATE requests are sent with item data:
api.leftApp.fetch(url) - fetch data from endpoint from LEFT side app
api.leftApp.post(url, postData) - post data to LEFT side app
api.rightApp.fetch(url) - fetch data from endpoint from RIGHT side app
api.rightApp.post(url, postData) - post data to RIGHT side app
api.log(string) - write a log line to log file of the run
state.syncAction - Create or Update
state.reqObj - request object data (containing fields) that was prepared by GetInt
state.triggerObj - data of the trigger/source object
For instance, if ITEM-1 was modified in Jira and will be synced with Azure Work Item #32, state.triggerObj contains data of ITEM-1 and state.reqObj is a constructed data object that will update #32
Before a comment is created
api.leftApp.fetch(url) - fetch data from endpoint from LEFT side app
api.leftApp.post(url, postData) - post data to LEFT side app
api.rightApp.fetch(url) - fetch data from endpoint from RIGHT side app
api.rightApp.post(url, postData) - post data to RIGHT side app
api.log(string) - write a log line to log file of the run
state.comment - comment data that was prepared by GetInt to send
If state.comment.skipped flag is set up, an equal true comment will be skipped and the request won't be sent.
Mapping SNOW User ID to Jira Username
To ensure proper synchronization between SNOW and Jira when the SNOW User ID equals the Jira username, use the following scripts. Make sure the default mapping for both sides is set to Use value from the other side
Jira Side Script
The following script should be used on the Jira side to map the ServiceNow user correctly:
if (state.reqObj.fields && state.reqObj.fields.assignee) {
var userIdentifier = state.reqObj.fields.assignee.name;
var response = api.rightApp.fetch('/api/now/v2/table/sys_user?sysparm_query=sys_id=' + userIdentifier + '&sysparm_fields=user_name');
if (response && response.result && response.result[0]) {
api.log('found snow user ' + response.result[0].user_name + ' by searching for ' + userIdentifier);
state.reqObj.fields.assignee.name = response.result[0].user_name;
}
}
ServiceNow Side Script
The following script should be used on the SNOW side to map the Jira user correctly:
if (state.reqObj.assignee) {
var userIdentifier = state.reqObj.assignee.name;
var response = api.rightApp.fetch('/api/now/v2/table/sys_user?sysparm_query=sys_id=' + userIdentifier + '&sysparm_fields=user_name');
if (response && response.result && response.result[0]) {
api.log('found snow user ' + response.result[0].sys_id + ' by searching for ' + userIdentifier);
state.reqObj.assignee.name = response.result[0].user_name;
}
}
By using these scripts, you can effectively map ServiceNow User IDs to Jira usernames, ensuring seamless integration and synchronization between your systems.
Example
Migrating GitLab issue to Jira. Except for field data we want to copy to Jira information about related merge requests to the given GitLab issue. GitLab offers API where we can that necessary information and obtain Pull Requests data in JSON format. Jira is on the left side in integration and GitLab is on the right side. To achieve what we need we have to put below JavaScript code below under the Before item request object sent (left app) option.
// 1 - fetch merge requests data frmo rightApp (GitLab)
var links = api.rightApp.fetch('/api/v4/projects/' + state.triggerObj.project_id + '/issues/' + state.triggerObj.iid + '/related_merge_requests');
if (links) {
// 2 - iterate over links and add to the array
for (var i=0; i<links.length; i++) {
var link = links[i].web_url;
linksArray.push('[' + link + '|' + link + ']');
}
}
// 3 - join links together and insert into a customfield
state.reqObj.fields['customfield_10174'] = mergesArray.join('\n');
If you encounter any issues or need further assistance, please contact our support team here. Our fantastic team at Getint is always ready to assist you!