Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Change Priority Based on Single List choice

Drishti Maharaj
Contributor
January 25, 2022

Hi,

 

I found this script and I am trying to adapt it to my needs and add it as a post function to the create step in my workflow so that when an issue is logged, it looks at a field and then updates the priority accordingly.

Custom Field - Cyber Incident Categorisation with the following options:
Account Compromise - P1
Host Compromise - P2
Phishing - P3
Denial of Service - P4
Extortion (Ransomware)  - P5
Insider Threat - P1
Data Loss & Compromise - P2

Script:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.PriorityManager
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption

final customFieldName = "Cyber Incident Categorisation"

//Get custom field issue with this name
def customFieldManager = ComponentAccessor.customFieldManager
def customField = customFieldManager.getCustomFieldObjects(issue).findByName(customFieldName)
assert customField: "Could not find custom field with name $customFieldName"

def customFieldVal = issue.getCustomFieldValue(customField) as LazyLoadedOption

//Create priorities
def priorities = ComponentAccessor.getComponent(PriorityManager).priorities

def highestPriority = priorities.findByName("P1")
def highPriority = priorities.findByName("P2")
assert highestPriority && highPriority: "One ore more selected priorities doesn't exist"

--- I actually need this to go up until P5 so would the additional priorities need to be added on and defined as such as medium, lower, lowest?

 //Assign priority depending on the custom field value
switch (customFieldVal?.value) {
case "Account Compromise":
issue.setPriorityId(highestPriority.id)
break
case "Host Compromise":
issue.setPriorityId(highPriority.id)
break
default:
log.debug "No need to change the priority"
break
}

 

Any help would be appreciated.

 

Thanks.

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Antoine Berry
Community Champion
January 26, 2022

Hello @Drishti Maharaj ,

I just tried your script. It is working fine, I only had to add this line at the end to update the issue : 

ComponentAccessor.getIssueManager().updateIssue(ComponentAccessor.getUserManager().getUserByKey('user_key'), issue, EventDispatchOption.DO_NOT_DISPATCH, false);

Of course you will need to map all the priorities in your switch but that does not seem to be the issue.

Let me know if that helped.

Drishti Maharaj
Contributor
January 26, 2022

Hi @Antoine Berry , thank you for taking the time to reply, if I add the additional script from you, the following error occurs:

Screenshot 2022-01-27 082910.png

Drishti Maharaj
Contributor
January 26, 2022

Hi @Antoine Berry , I managed to figure out what was wrong :)

Just needed this at the top:

import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.event.type.EventDispatchOption

and it worked perfectly! 

Thank you so much!

Like Antoine Berry likes this
TAGS
AUG Leaders

Atlassian Community Events