Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Servicenow to Jira integration with using flow actions

Jayesh 315
Tera Contributor

For the integration I have first created UI Action.
______________________________________________________________

function createJiraTicket() {
   g_form.addInfoMessage("Creating Jira Ticket..."); // Notify the user about the action
   var ga = new GlideAjax("JiraIntegrationScript"); // Script Include name
   ga.addParam("sysparm_name", "createJiraTicket"); // Method name in Script Include
   ga.addParam("sysparm_current_sysid", g_form.getUniqueValue()); // Pass current record sys_id
   ga.getXMLAnswer(function(response) {
       try {
           // Use getAnswer to get the response
           var answer = JSON.parse(response);
           if (answer.status === 200) {
               g_form.addInfoMessage(answer.message); // Display success message
           } else {
               g_form.addErrorMessage(answer.message); // Display error message
           }
       } catch (error) {
           g_form.addErrorMessage("Unexpected error occurred: " + error.message);
           gs.info("Error parsing response: ", error);
       }
   });
}

Jayesh315_1-1738170797394.png

 

 

Then I have created the Script Include :
____________________________________________________________________

var JiraIntegrationScript = Class.create();
JiraIntegrationScript.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
   createJiraTicket: function() {
       var finalResult = {};
       try {
           // Step 1: Get the catalog item sys_id from the request
           var catItemSysId = this.getParameter('sysparm_current_sysid');
           if (JSUtil.nil(catItemSysId)) {
               finalResult.status = 400;
               finalResult.message = "Catalog Item sys_id is missing.";
               return JSON.stringify(finalResult);
           }
           // Step 2: Validate catalog item existence
           var catItemGR = new GlideRecord('sc_cat_item');
           if (!catItemGR.get(catItemSysId)) {
               finalResult.status = 404;
               finalResult.message = "Catalog item not found for sys_id: " + catItemSysId;
               return JSON.stringify(finalResult);
           }
           // Step 3: Fetch catalog item variables
           var variables = {};
           var optionGR = new GlideRecord('item_option_new'); // 'item_option_new' stores catalog item variables
           optionGR.addQuery('cat_item', catItemSysId);
           optionGR.query();
           while (optionGR.next()) {
               variables[optionGR.name.toString()] = optionGR.default_value.toString();
           }
           // Step 4: Prepare Jira ticket details
           var summary = "Test Jira Issue: " + (variables.short_description || "No summary provided");
           var description =
               "Quantifiable Business Benefit: " + (variables.quantifiable_business_benefit || "N/A") + "\n" +
               "What is the Problem: " + (variables.what_is_the_problem || "N/A") + "\n" +
               "Primary Business Benefit: " + (variables.primary_business_benefit || "N/A") + "\n" +
               "Requesting Function: " + (variables.requesting_function || "N/A");
           var projectKey = "19310"; // Example project key
           var issueType = "10001"; // Example issue type
           var workType = "Change";
           var planned = "Planned";
           // Log summary and description
           gs.info("Jira Ticket Summary: " + summary);
           gs.info("Jira Ticket Description: " + description);
           // Validate mandatory fields
           if (JSUtil.nil(projectKey) || JSUtil.nil(issueType) || JSUtil.nil(summary)) {
               finalResult.status = 400;
               finalResult.message = "Missing mandatory fields: projectKey, issueType, or summary.";
               return JSON.stringify(finalResult);
           }
           // Step 5: Execute Jira Subflow
           var inputs = {
               project_id: projectKey,
               issue_type_id: issueType,
               summary: summary,
               description: description,
               customfield_13411: workType,
               customfield_12715: planned
           };
           var result = sn_fd.FlowAPI.getRunner()
               .action('sn_jira_spoke.create_issue_v4') // Replace with your subflow action name
               .inForeground()
               .withInputs(inputs)
               .run();
           // Step 6: Check the result
           var outputs = result.getOutputs();
           if (outputs.issue && outputs.issue.key) {
               finalResult.status = 200;
               finalResult.message = "Jira issue created successfully: " + outputs.issue.key;
           } else {
               finalResult.status = 500;
               finalResult.message = "Jira issue creation failed, no issue key returned.";
           }
       } catch (ex) {
           gs.error("Error creating Jira ticket: " + ex.getMessage());
           finalResult.status = 500;
           finalResult.message = "Unexpected error occurred: " + ex.getMessage();
       }
       return JSON.stringify(finalResult);
   }
});
I am using this flow action :
Jayesh315_2-1738171160110.png

 

I am getting an error in the when I click on the button  "Create jira ticket".

Jayesh315_0-1738170719859.png

 

1 REPLY 1

Community Alums
Not applicable

hi @Jayesh 315 ,

. Check the Client Script (UI Action) for Errors

  • Ensure that the UI Action is triggering the function correctly.
  • Add some debugging logs in the client-side createJiraTicket() function to verify the flow.

 

function createJiraTicket() {
    g_form.addInfoMessage("Creating Jira Ticket..."); // Notify the user about the action
    console.log("UI Action triggered successfully");
    var ga = new GlideAjax("JiraIntegrationScript"); // Script Include name
    ga.addParam("sysparm_name", "createJiraTicket"); // Method name in Script Include
    ga.addParam("sysparm_current_sysid", g_form.getUniqueValue()); // Pass current record sys_id
    ga.getXMLAnswer(function(response) {
        console.log("Response from Script Include: ", response); // Add debug log
        try {
            var answer = JSON.parse(response);
            if (answer.status === 200) {
                g_form.addInfoMessage(answer.message); // Display success message
            } else {
                g_form.addErrorMessage(answer.message); // Display error message
            }
        } catch (error) {
            g_form.addErrorMessage("Unexpected error occurred: " + error.message);
            console.error("Error parsing response: ", error);
        }
    });
}

 

 

2. Test the Script Include

  • Check the Script Include by calling the method directly with sample inputs in a script debugger or Background Script.
  • Example Background Script: 
  • var script = new JiraIntegrationScript();
    var response = script.createJiraTicket();
    gs.info("Response: " + response);

 

. Verify Inputs for the Jira Subflow

  • Check the inputs to the sn_jira_spoke.create_issue_v4 action.
  • Ensure project_id, issue_type_id, summary, description, and other required inputs are valid and match the Jira configuration.
  • Add debugging logs before executing the flow to confirm the inputs.

 

javascript
CopyEdit
gs.info("Inputs to Jira Subflow: " + JSON.stringify(inputs));

 

 

4. Check the Flow Action Execution

  • If the subflow (sn_jira_spoke.create_issue_v4) fails, inspect the flow execution logs:
    • Navigate to Flow DesignerExecution Details.
    • Look for failed executions and error messages in the details.

5. Verify Catalog Item Variables

  • Ensure the item_option_new table has the variables you expect. Missing or incorrect variables could result in errors.
  • Add debugging logs to verify the fetched variables.

 

javascript
CopyEdit
gs.info("Fetched Variables: " + JSON.stringify(variables));

 

 

6. Check the Response from Jira Subflow

  • Validate the response from the Jira subflow using logs:

 

javascript
CopyEdit
var outputs = result.getOutputs(); gs.info("Outputs from Jira Subflow: " + JSON.stringify(outputs));