Configure Priority field based on issue type
Asked by David Whitlock:

Hello, As a Jira Admin, I need to be able to display a set of Custom Priorities based the issue type selected and set a default value. I have looked into testing with ScriptRunner behaviors and although I get the behavior I want, it is not without its glitches. The behavior to filter out the priority list looks like the below:

import com.atlassian.jira.component.ComponentAccessor  
import com.atlassian.jira.issue.Issue  
import com.atlassian.jira.issue.priority.Priority

// Get the current issue type  
def issueType = getIssueContext().getIssueType().getName()

def prioritySet = null  
def filteredPriorities = null  
if (issueType.matches("Bulk Upload\|Business Story\|Database Task\|DataMigration\|Deployment\|Epic\|Feature\|Improvement\|New Feature\|Operational Support\|QE Task\|Request\|Research Spike\|Story\|Task\|Technical Story\|Test\|UAT Task\|UI / Content")){  
def nonDefectPriorities = \[ "1-Must Have" , "2-Should Have" , "3-Could Have" , "4 - Will not Have " \]  
prioritySet = nonDefectPriorities  
}else {  
def defectPriorities = \[ "P0 - Blocker (SN P1-Critical)" , "P1 - Critical (SN P2-High)" , "P2 - Major (SN P3-Medium)" , "P3 - Medium (SN P4-Low)" , "P4 - Minor" , "P5 - Trivial" \]  
prioritySet = defectPriorities  
}

// Get the priority field  
def priorityField = getFieldById("priority")

// Get all available system priorities  
def priorities = ComponentAccessor.constantsManager.priorities

// Filter the priority list  
filteredPriorities = priorities.findAll {  
Priority priority -\> prioritySet.contains(priority.name)  
}  
// Set default priority list  
priorityField.setFieldOptions(filteredPriorities)

The problem with this behavior is that if the issue type is changed a warning is thrown that the default value is preserved. Per Adaptavist this is expected and no way can it be suppressed without suppressing all warning messages. I want to avoid this because I know I will get many requests that Jira is throwing an error.

The other behavior is to set a default value for a Priority based on issue type. This behavior has a workflow action condition on Create

import com.atlassian.jira.component.ComponentAccessor  
import com.atlassian.jira.issue.Issue  
import com.atlassian.jira.issue.MutableIssue  
import com.atlassian.jira.issue.priority.Priority

// Get the current issue type  
def issueType = getIssueContext().getIssueType().getName()

// Get the priority field  
def priorityField = getFieldById("priority")

if (issueType.matches("Bulk Upload\|Business Story\|Database Task\|DataMigration\|Deployment\|Epic\|Feature\|Improvement\|New Feature\|Operational Support\|QE Task\|Request\|Research Spike\|Story\|Task\|Technical Story\|Test\|UAT Task\|UI / Content")){  
// Set default priority based on issue type  
priorityField.setFormValue("3-Could Have") // Sets priority to 3-Could Have

}else if(issueType.matches("Bug\|Defect\|Production Defect\|QE Defect\|Security Defect\|UAT Defect\|Warranty Defect")){  
priorityField.setFormValue("P4 - Minor") // Sets priority to P4

}else{  
// Set the subtasks priority as the parent issue's priority  
String parentIssueId = getFieldById("parentIssueId").getFormValue()  
if (parentIssueId) {  
MutableIssue parentIssue = ComponentAccessor.issueManager.getIssueObject(new Long(parentIssueId))  
if (parentIssue) {  
def parentPriority = parentIssue.priority  
priorityField.setFormValue(parentPriority.id)  
}  
}  
}

This works, however when I open an existing issue to edit, the prirority is being reset. Does anyone have any suggestions on how to better accomplish what I am attempting to do? Thanks.


Answers:
author: Phil Arnold
body: Not sure if this is applicable to your scenario but I had a similar requirement however my solution involved me creating a custom field and then creating different contexts for the custom field based on project and issue types.

See here for more info - <https://support.atlassian.com/jira-cloud-administration/docs/configure-custom-field-context/>


Tags: data-center

jira-data-center

jira-server

server