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 | ||||
IMPACT | Critical | High | Medium | Low |
Extensive | P1-Critical priority | P2-High priority | P3-Medium priority | P4-Low priority |
Significant | P1-Critical priority | P2-High priority | P3-Medium priority | P4-Low priority |
Moderate | P2-High priority | P3-Medium priority | P4-Low priority | P4-Low priority |
Minor | P3-Medium priority | P4-Low priority | P4-Low priority | P4-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,
Can should share some extra details on your listener configuration?
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()
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,
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Peter-Dave Sheehan ,
I updated and pasted the code under the "If the following condition evaluates to true"
Cheers,
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
And I checked the log. It says missing urgency or impact
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Did you make sure the fields name match with the correct case?
it my original example I had impact with lowercase i.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
I'll do few more testing before implementing this script in actual live project.
Cheers,
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
Best, Joseph Chung Yin
Jira/JSM Functional Lead, Global Technology Applications Team
Viasat Inc.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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,
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Behavior for Scriptrunner for Jira (Cloud) is available - You must have Scriptrunner for Jira add-on first then you can install the Behavior add-on for free -
Here is documentation for Behavior (Cloud) -
It should not matter if you are only using JSM and not Jira Software.
Hope this helps.
Best, Joseph
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.