high speed issue linking in Jira Service Desk
November 06, 2018

High speed issue linking in Jira Service Desk

PF
Phill Fox 7 minute read

This is the third in our Incident Management blog series, aimed at making your Incident resolution quicker and your customers happier. This time we look at how you can link your Incidents, Alerts and Remedial Actions to current and recent Problems, using a self-referencing transition in your workflow.

This instalment covers linking related Issues together for better diagnostics and management.

In order to create this functionality, we need to reuse some of the configurations that we built in the previous two articles. Let's quickly recap on what we covered in these two articles and what we'll reuse.

In the first blog, we created new Issuetypes and established appropriate links between them. We'll be using both the Issuetypes and links for this functionality.

In the second blog we saw how we could use JQL to find unresolved and recently-resolved problems. We will be reusing the following JQL to restrict our selection to current and recent Problem Issuetypes.

Note: If you've given different names to your Issuetypes or links, you will need to update the code that follows to match.

What linking achieves

Linking related Issues together is a powerful feature of Jira which allows you to better diagnose Issues and manage your work. If you're a frequent Jira user, you would have likely linked Issues before and have probably found it to be manual and labour-intensive. A process that requires you to know exactly what Issues should be linked and to manually complete forms.

What if we could speed up this process and reduce the potential for Issues to be missed or incorrectly connected?

Linking the problem ticket

The first step in linking tickets is to create a custom field. This custom field needs to be set as a Hideable Text Field and labeled as "Known Problem". To build this Hideable Text Field select the <255 characters option in the Advanced Select Field Type. We're selecting this option as we only need to store the Problem Issue key in the field which should never be anywhere near 255 characters in length.

pic 1

 

Next, we want to make sure that this field is only available for the following Issuetypes Incident, Alert, Remedial Action. This means that we need to set the configuration to only these three Issuetypes. At this stage, we could also restrict this functionality to certain projects but for this example, we're only restricting our use of the Issuetypes with an Issuetype Scheme at the project level.

pic 2

Now we have the custom field, we will create a screen called “Link to recent Problem” that we can use on the transition. For this screen, we are going to keep the information requested to the Known Problem field that we have just created and of course the standard comment field.

Updating your workflow

After we've added our screen, the next step is to edit the workflow(s) that are associated with the Issuetypes Incident, Alert, and Remedial Action. We're going to use a little-known but powerful piece of functionality, the ability for a workflow to transition from any status back to the same status. While adding a circular workflow sounds rather odd, critically it allows us to add a transition and where you have a transition you can add a transition screen and post-function. This appears on the workflow diagram as shown.

pic 3

Creating a transition

So let's look at the steps to create this transition. First, we'll add a transition and fill in the options as shown below.

 

pic 4

Note: The two status fields use special values that don't refer to the single status.

After that, we need to add the following condition to the workflow to make sure this is only available for the appropriate Issuetypes.

pic 5

 

We could also add a property to the transition to place the transition as the first option.

pic 6 700x213

 

The final step in the workflow stage is to publish it. Once we've done this, remember to check that the right screen loads on the transition.

Adding behaviours

Our next step is to add the behaviour to our new field so that it only offers the choices from current or recent Problems. In order to add the behaviour navigate to:

<yourhost>/secure/admin/Behaviours!default.jspa

Select create a new behaviour and give your new behaviour the following name and description:

link to existing problem

Then map the following fields:

behavior ID

Next, add an initialiser function with the following code. This initialiser function will be called whenever the field is presented on a screen for editing.

 

getFieldByName("Known Problem").convertToSingleSelect([ 

ajaxOptions: [

  url : getBaseUrl() + "/rest/scriptrunner-Jira/latest/issue/picker",

  query: true, // keep going back to the sever for each keystroke

  // this information is passed to the server with each keystroke

  data: [

    currentJql: "issuetype=Problem AND (resolution is EMPTY OR resolutiondate > -7d) ORDER BY resolution, key ASC",

    label     : "Pick from Open or Recent Problems",

    showSubTasks: false,

    // specify maximum number of issues to display, defaults to 10

    max: 5,

    ],

     formatResponse: "issue"

  ],

  //Specify css for the size of the dropdown maximum width

  css: "max-width: 500px; width: 500px",

]

)

Lastly, place a listener on the custom field in order to make sure that whenever a value is entered the links on the issue are updated to include the related Problem.

  
// Constants
final PROBLEM_FIELD = "customfield_10501"
final INCIDENT_LINK = "Incident Link"
final ALERT_LINK = "Alert Link"
final REMEDIAL_LINK = "Remedial Action Link"
  
def createIssueLink(Issue startIssue,
            Issue issueToLink,
            Long linkTypeId,
            Direction linkDirection,
            ApplicationUser updateUser) {
  
  // Get the issue service
  
  // Validate the link creation
  final addIssueLinkValidationResult =
                  startIssue,
                  linkTypeId,
                  linkDirection,
                  [issueToLink.key],
                  true)
  
  // Create the actual link
     issueLinkService.addIssueLinks(updateUser, addIssueLinkValidationResult)
  }
}
  
List<Issue> getLinkedIssues(Issue rootIssue,
          String linkType) {
  
  // Managers
  final issueLinkManager = ComponentAccessor.issueLinkManager
  
  // The list to hold our found issues
  def outIssues = []
  
  // Get the outward links of the issue and find the matching ones
  final foundOutLinks = outwardLinks.findAll {
     it.issueLinkType.name in linkType
  }
  
  // Find the linked issues and return
  return outIssues
}
  
// Managers
final customFieldManager = ComponentAccessor.customFieldManager
final issueManager = ComponentAccessor.issueManager
  
// Get the issue from the event and get the value fo the field
final issue = event.issue
final issueFieldValue  = customField.getValue(issue) as String
  
//Create a variable to hold the appropriate link depending on whether the current issue is an incident or an aler
  
def PROBLEM_LINK = ""
    PROBLEM_LINK = INCIDENT_LINK
}else if(issue.getIssueType().getName() == "Alert"){
    PROBLEM_LINK = ALERT_LINK
}else if(issue.getIssueType().getName() == "Remedial Action"){
    PROBLEM_LINK = REMEDIAL_LINK
}
  
//If we have a Problem_Link, check for new or existing links
if (PROBLEM_LINK != "") {
// If the customfield has a value, do something
  if ( issueFieldValue) {
  
  // Get all the linked issues and exit if the issue is already one
  final linkedIssues = getLinkedIssues(issue, PROBLEM_LINK).collect{it.key}
  if (issueFieldValue in linkedIssues) {return}
  
  // Get the bits we need for the link
  final currentUser = authContext.loggedInUser
  
  // Make the link
  createIssueLink(issue, linkIssue, issueLink.id, Direction.OUT, currentUser)
  
  }
}

You should now test that your system is working and that when a new Known Problem is associated with an Incident, Alert or Remedial Action the Issue links are updated correctly.

Once it is, you'll be able to group your incidents with much more ease, increasing your diagnostic abilities and allowing for better organisation of your Triage.

Our next tip looks at how we can quickly create a new problem ticket from inside an Incident or Alert. To find out more about how ScriptRunner for Jira can help you extend, automate, and customise your Jira instance, start a free 30 day trial on the Atlassian Marketplace.

Read the next blog