Help with groovy scriptrunner calculated field for non-programmer

Seve Ponce de Leon October 9, 2019

Hi guys, I have read over many examples as well as the documentation but at the end of the day I'm not a programmer so am out of luck.

I have 3 custom fields:

  • Likelihood, a single choice select list with text A, B, C
  • Severity, a single choice select list with text D, E, F
  • Rating, a scriptrunner calculated field with text

I just want to set the Rating based on what has been chosen for Likelihood and Severity, there are really just 9 combinations:

  • If Likelihood = A and severity = D set the Rating to 1
  • If Likelihood = A and severity = E  set the Rating to 2
  • If Likelihood = A and severity = F  set the Rating to 3
  • If Likelihood = B and severity = D set the Rating to 4
  • If Likelihood = B and severity = E  set the Rating to 5
  • If Likelihood = B and severity = F set the Rating to 6
  • If Likelihood = C and severity = D set the Rating to 7
  • If Likelihood = C and severity = E set the Rating to 8
  • If Likelihood = C and severity = F set the Rating to 9

I tried loading up something like this but the preview just ends up null:

---

import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def likelihood = customFieldManager.getCustomFieldObjectByName("Likelihood")
def severity = customFieldManager.getCustomFieldObjectByName("Severity")
def rating = customFieldManager.getCustomFieldObjectByName("Rating")

def impactValue = issue.getCustomFieldValue(severity)
def probabilityValue = issue.getCustomFieldValue(likelihood)
def ratingValue = issue.getCustomFieldValue(rating)

if ((impactValue="A") && (probabilityValue="D")){
rating="1"
}

Thanks for your help

2 answers

1 vote
Jack Nolddor _Sweet Bananas_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
October 9, 2019

Hi,

Im pretty sure you will find really useful my Urgency / Impact matrix example:

 

/*
* @Author: Jack Nolddor [Sweet Bananas] <support@sweetbananas.es>
* @Date: 09-Oct-2019
*
* @Description:
* Basic implementation of a priority matrix using impact and urgency values
* You must use Text Field (multi-line) or Number Field Template
*
* See the links below for further details:
* https://confluence.atlassian.com/servicedeskserver/calculating-priority-automatically-939926661.html
* https://community.atlassian.com/t5/Jira-questions/Help-with-groovy-scriptrunner-calculated-field-for-non/qaq-p/1199242
*/

import org.apache.log4j.Logger
import org.apache.log4j.Level
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.atlassian.jira.issue.CustomFieldManager


def log = Logger.getLogger("com.nolddor")
log.setLevel(Level.DEBUG)

@PluginModule
CustomFieldManager customFieldManager


//--------------------------------------------------------------
// You must change the following variables as your needs
//--------------------------------------------------------------
def impactFieldName = "Impact"
def urgencyFieldName = "Urgency"

def matrix = [
    [impact: 'A', urgency: 'D', priority: '1'],
    [impact: 'A', urgency: 'E', priority: '2'],
    [impact: 'A', urgency: 'F', priority: '3'],
    [impact: 'B', urgency: 'D', priority: '4'],
    [impact: 'B', urgency: 'E', priority: '5'],
    [impact: 'B', urgency: 'F', priority: '6'],
    [impact: 'C', urgency: 'D', priority: '7'],
    [impact: 'C', urgency: 'E', priority: '8'],
    [impact: 'C', urgency: 'F', priority: '9'],
]
//--------------------------------------------------------------


//Retrieve customfields from the system
def impactCF = customFieldManager.getCustomFieldObjectByName(impactFieldName)
def urgencyCF = customFieldManager.getCustomFieldObjectByName(urgencyFieldName)

// Ensure source fields really exist
if(impactCF && urgencyCF)
{
    // Retrieve customfield values as String
    def impact = issue.getCustomFieldValue(impactCF).toString()
    def urgency = issue.getCustomFieldValue(urgencyCF).toString()
    // Check for any match on our matrix
    def row = matrix.find{ row -> row.impact == impact && row.urgency == urgency}
    // Return the priority for the matching row (if any)
    return row?.priority
}

 

 


Don't forget to modify the variables to fits your needs :)
Happy Coding!

Seve Ponce de Leon October 10, 2019

As soon as I paste in your code my jira instance crashes and I have to restart it, I can't even start modifying it. Any ideas?

Jack Nolddor _Sweet Bananas_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
October 10, 2019

Hi Seve, that was just an example.

 

You need to modify the code as your own to fits your needs. Don't forget to first tested it in your stagging env.

Be sure you are using the latest Script Runner version to ensure full compatibility.
Regards

Seve Ponce de Leon October 10, 2019

I got that, I just thought I could first paste and then modify. I couldn't even click the Preview button, jira just stopped working immediately after pasting. Any guesses as to why? I'll try modifying in an editor and then pasting...

Jack Nolddor _Sweet Bananas_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
October 10, 2019

Have you tried on a different browser version? or computer?

0 votes
Ben Poulson
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 11, 2019

I believe I see the problem. A scripted field works like this: the field itself doesn't actually have a value, per se. Instead, the field has a script attached to it. Whenever Jira needs a value to display for that field (such as when you load up the page for an issue), it runs the script and uses the value that the script gives it. In programming, the concept of sending a value when asked for it is called "returning". A return statement essentially marks the end of that block of code (in this instance, the script). Think of it like buying a digital movie from Amazon. When you're purchasing the movie, you give them the information they need to do their job (Name of the movie, credit card number, billing address, etc.). In this analogy, the info you give to Amazon is called parameters. Your script takes parameters as well, even though scriptrunner obfuscates it. In this case, the parameters that your script takes is the issue itself, and all of its associated information (various fields, when the issue was created, etc.). At the end the transaction with a cashier, you expect to get the movie you requested. Likewise, jira is expecting something from your script- a value to put in the field. This is the code you are currently using to set the rating:

 

def ratingValue = issue.getCustomFieldValue(rating)
//other code omitted
rating = "1"

The reason this doesn't work is that it's not actually modifying the value of the field in the issue. On the line where you declare the rating variable (def rating = ...), that's essentially making a copy of the value of that field. To go back to the Amazon analogy: when you buy a digital movie from Amazon, you're buying a copy of the movie, not the actual movie itself- if you load the movie into Windows Movie Maker and edit it, it only affects your copy of the movie. If someone else buys that same movie or you decide to redownload it, that version won't have your edits, since you were only modifying that 1 copy. The problem with your script is similar. When that line asks the issue for the value of the rating field, the issue is giving a copy of that value, not the original value, which frees you up to manipulate that value as you see fit within your script (to use in calculations, for example) without breaking the actual value in the issue. when you set the rating to 1, that only affects your 1 copy of it, not the original. 

 

All of this is a long winded way of saying this: replace

rating = "1"

with

return "1"

This is how you make your script actually give a value back to Jira to use in the field.

Suggest an answer

Log in or Sign up to answer