Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Update priority based on urgency and impact through ScriptRunner

Zaldy Parian
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.
November 9, 2023

Hi all,

First time user of ScriptRunner here. I searched the Community field for similar situation as mine but cannot find one that works for me. Anyway, I got 2 fields that set the priority on -- urgency and impact. The priority is defaulted to 'P4 - Low'. The matrix is like this....

 URGENCY
IMPACTCriticalHighMediumLow
ExtensiveP1-Critical priorityP2-High priorityP3-Medium priorityP4-Low priority
SignificantP1-Critical priorityP2-High priorityP3-Medium priorityP4-Low priority
ModerateP2-High priorityP3-Medium priorityP4-Low priorityP4-Low priority
MinorP3-Medium priorityP4-Low priorityP4-Low priorityP4-Low priority

And my script which I added in the Script Listener is this...

def urgency = issue.get("customfield_10034")
def impact = issue.get("customfield_10004")

def priority = ""

if (urgency == "Critical" && impact == "Extensive") {
priority = "P1 - Critical"
} else if (urgency == "Critical" && impact == "Significant") {
priority = "P1 - Critical"
} else if (urgency == "Critical" && impact == "Moderate") {
priority = "P2 - High"
} else if (urgency == "Critical" && impact == "Minor") {
priority = "P3 - Medium"
} else if (urgency == "High" && impact == "Extensive") {
priority = "P2 - High"
} else if (urgency == "High" && impact == "Significant") {
priority = "P2 - High"
} else if (urgency == "High" && impact == "Moderate") {
priority = "P3 - Medium"
} else if (urgency == "High" && impact == "Minor") {
priority = "P4 - Low"
} else if (urgency == "Medium" && impact == "Extensive") {
priority = "P3 - Medium"
} else if (urgency == "Medium" && impact == "Significant") {
priority = "P3 - Medium"
} else if (urgency == "Medium" && impact == "Moderate") {
priority = "P4 - Low"
} else if (urgency == "Medium" && impact == "Minor") {
priority = "P4 - Low"
} else {
priority = "Low"
}

return priority

But when I test it during create or edit mode, nothing happens.

 

Please advise.

Thanks,

2 answers

1 accepted

0 votes
Answer accepted
Peter-Dave Sheehan
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.
November 10, 2023

Can should share some extra details on your listener configuration?

  • What event is is listening to?
  • And is this all of your code? Where is the part that actually sets the priority field?

I have a similar situation and it can be greatly simplified with the use of groovy map/data structure.

I'm not super well versed in cloud scripts, but I took my best shot at building a listener that will take the urgency and impact (just like in my sever environment) to determine the correct priority and then update the issue with that priority

// get custom fields so we can find the id of the urgency and impacts
def customFields = get("/rest/api/2/field")
.asObject(List)
.body
.findAll { (it as Map).custom } as List<Map>

def urgencyCfId = customFields.find { it.name == 'Urgency' }?.id
def impactCfId = customFields.find { it.name == 'impact' }?.id

//this was added by the scriptrunner cloud example as a way to check the listener is only applied to the correct project
def projectKey = "TP" //update to match the expected project
if (issue == null || ((Map) issue.fields.project).key != projectKey) {
logger.info("Wrong Project ${issue.fields.project.key}")
return
}

//get the values from your issues
def urgency = issue.fields[urgencyCfId] as Map
def impact = issue.fields[impactCfId] as Map

if (urgency == null || impact == null) {
logger.info("Missing urgency or impact. Can't set priority")
return
}

//this is the simplified matrix
def matrix = [
Critical: [Extensive: '1', Significant: '2', Moderate: '3', Minor: '4'],
High : [Extensive: '1', Significant: '2', Moderate: '3', Minor: '4'],
Medium : [Extensive: '2', Significant: '3', Moderate: '4', Minor: '4'],
Low : [Extensive: '3', Significant: '4', Moderate: '4', Minor: '4']
]
//convert the simple numbers into full priority name. Alternatively, skip this part and and plug in the priority ids in the matrix above
def priorities = [
'1': 'P1-Critical priority',
'2': 'P2-High priority',
'3': 'P3-Medium priority',
'4': 'P4-Low priority'
]
def priorityNumber = matrix[urgency.value][impact.value]
def priorityName = priorities[priorityNumber]
//I tried to use the queryString("priorityName", priorityName) but it was not working
def priority = get("/rest/api/2/priority/search")
.queryString("projectId", issue.fields.project.id)
.asObject(Map)
.body.values.find { (it as Map).name == priorityName }

//update the issue with the new priority
put("/rest/api/2/issue/${issue.key}")
.header("Content-Type", "application/json")
.body([
fields: [
"priority": priority
]
])
.asString()
Zaldy Parian
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.
November 12, 2023

Hi @Peter-Dave Sheehan ,

I appreciate you help with this. I tried to update your recommended script and run a couple of changes in the actual issure record. Then I checked history log of the script. I seems to have an error. The log at the end has this....

 

evaluated to: ["Jira expression failed to parse: line 2, column 5:\n=, =>, ., ?., [, ?.[, (, *, /, %, +, -, <, <=, >, >=, ==, !=, &&, ||, ??, ?, ; or EOF expected, customFields encountered."]

 Do you know what might be causing the error?

 

Regads,

Peter-Dave Sheehan
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.
November 12, 2023

Where did you implement that script?

It should be in a Script Listener and associated with the "issue updated" and "Issue created" events.

Copying that script as in (nearly) the "code to run" section of the scriptrunner script listener worked like a charm.

THe only change I made to the script was adjust the field names and project key filter.

Zaldy Parian
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.
November 12, 2023

Hi @Peter-Dave Sheehan ,

I updated and pasted the code under the "If the following condition evaluates to true"

scriptrunner_1.png

Cheers,

Zaldy Parian
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.
November 12, 2023

Hi again @Peter-Dave Sheehan ,

I moved the script to "Code to run" space. And updated the Urgenct and Impact of an issue in my TEST project. 

Although the priority remains unchanged, when I look at the script, I noticed there were green ticks (as opposed to red Xs earlier).  I guess we are going to the right direction. I just need to figure out why the priority did not change.

scriptrunner_2.png

Zaldy Parian
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.
November 12, 2023

And I checked the log. It says missing urgency or impact

scriptrunner_3.png

Like Joseph Chung Yin likes this
Peter-Dave Sheehan
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.
November 12, 2023

Did you make sure the fields name match with the correct case?

it my original example I had impact with lowercase i.

Zaldy Parian
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.
November 12, 2023

Hi @Peter-Dave Sheehan ,

I think I found the issue with the script. I have typed in the field name inaccurately. When I changed the field name to have capital letter at the start, then the auto setting of priority worked!

Thanks again for your patience and guidance. Same goes to @Joseph Chung Yin for his references.

scriptrunner_4.png

I'll do few more testing before implementing this script in actual live project.

 

Cheers,

0 votes
Joseph Chung Yin
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 10, 2023

@Zaldy Parian 

I am assuming that you are using the Behavior configuration to populate the Priority field value based on your ask, if so you will need to obtain the value of your two other fields in your code.  SImply just using issue.get is not enough in my opinion.

You should also create two additional variable to pull the value of those two fields 

Example -
def urgency = issue.get("customfield_10034")
def impact = issue.get("customfield_10004")
def urgencyValue = urgency.getValue()
def impactValue = impact.getValue()
Then use urgentcyValue and impactValue in your IF loop.
Lastly, you need to define Priority field, I don't believe you can simply just reference the field as "def priority = "" "
In addition you may want to contact Adaptavist (vendor of Scriptrunner for Jira) for further support against the Cloud env.
I am sure that you already found the the following references -
Hope this helps.

Best, Joseph Chung Yin

Jira/JSM Functional Lead, Global Technology Applications Team

Viasat Inc.

Zaldy Parian
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.
November 10, 2023

Hi @Joseph Chung Yin 

I appreciate your help and guidance.
At this point, I cannot use Behaviour as it is not yet available for JSM.

Yes, I did asked help from Adaptavist for this as well.

Cheers,

Joseph Chung Yin
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 10, 2023
Zaldy Parian
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.
November 11, 2023

Hi @Joseph Chung Yin ,

The Adaptavist support actually mentioned to me that Behaviour is still not available for JSM Cloud. In the documentation link you provided, there as section for supported platform, and JSM has a red X

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events