Forums

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

Assign Priority value based on custom fields using ScriptRunner Post Function

Dan Earle April 24, 2020

I've seen a bunch of other similar questions being asked but none of the provided code seems to work so I've had to cobble together this:

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

def CA = ComponentAccessor.getCustomFieldManager()
def fSev = issue.getCustomFieldValue(CA.getCustomFieldObject('customfield_10912'))
def iSev
if (fSev) {
iSev = ((LazyLoadedOption)fSev).getValue().toInteger()
} else { 1 }

def fImp = issue.getCustomFieldValue(CA.getCustomFieldObject('customfield_10914'))
def iImp
if (fImp) {
iImp = ((LazyLoadedOption)fImp).getValue().toInteger()
} else { 1 }

def num = ( iSev + iImp ) / 4

String pValue;
switch (num) {
case 1: pValue = "Lowest"; break;
case 2: pValue = "Low"; break;
case 3: pValue = "Medium"; break;
case 4: pValue = "High"; break;
case 5: pValue = "Highest"; break;
default: pValue = "Lowest"
}

def pfield = ComponentAccessor.getComponent(PriorityManager).priorities
def pID = pfield.findByName(pValue)

if (issue.issueType.name == 'Bug' ) {
issue.setPriorityId(pID.id)
}

 

There are 2 integers (iSev and iImp, both a value between 1 and 10), that this pulls from and 5 Priority levels (pValue). 

This code doesn't produce any errors, but it also doesn't set the priority value at all, what am I missing?

1 answer

0 votes
Leo
Community Champion
April 25, 2020

Hi @Dan Earle,

I used ConstantsManager to get priority which worked for me. this may give you some idea

import com.atlassian.jira.component.ComponentAccessor

def constantsManager = ComponentAccessor.getConstantsManager()
def field = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Custom Field")
def fValue = issue.getCustomFieldValue(field) as Integer
if(fValue == 1){
issue.setPriorityId(constantsManager.getPriorityObjects().findByName("P1").id)
}else{
issue.setPriorityId(constantsManager.getPriorityObjects().findByName("P3").id)
}

 

 

BR,

Leo

Suggest an answer

Log in or Sign up to answer