Create Listener that will change priority field based on a custom field

Matthew Zdonczik January 24, 2020

Hello,

I am trying to create a listener that will change the system field "Priority" to a certain value whenever I change the value of a custom field.

I am new to groovy so I don't really know where to start.

For example, my custom field has 10 values, 1-10. And I need to have the Priority field change based on one of those 10 values.

So say 1 = Major, 2 = Moderate etc

And I don't think I can use this a a post function as I need to be able to just edit the field and then have the priority change whether it is moving down the workflow or not.

Any help would be greatly appreciated.

Thanks!

1 answer

0 votes
Leo
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 24, 2020

Hi @Matthew Zdonczik,

 

Below Script would give you some idea

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueInputParametersImpl
import com.atlassian.jira.issue.Issue

def issue = event.issue as Issue
def constantsManager = ComponentAccessor.getConstantsManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getUser()
def issueService = ComponentAccessor.getIssueService()
def issueInputParameters = new IssueInputParametersImpl()

def cfField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Custom Field Name")
def value = issue.getCustomFieldValue(cfField) as Double // if it is a number field
if(value == 1){
issueInputParameters.setPriorityId(constantsManager.getPriorityObjects().findByName("P1").id)
}else if(value == 2){
issueInputParameters.setPriorityId(constantsManager.getPriorityObjects().findByName("P2").id)
}else{
issueInputParameters.setPriorityId(constantsManager.getPriorityObjects().findByName("P3").id)
}

def validationResult = issueService.validateUpdate(user, issue.id, issueInputParameters)

if (validationResult.isValid()) {
issueService.update(user, validationResult)
} else {
log.warn validationResult.errorCollection.errors
}

 

BR,

Leo

Matthew Zdonczik January 28, 2020

Hi @Leo thank you for your response!

This didn't quite work for me just yet.

My custom field a Select List (Single choice) issue type and the values are numbers.

Would that change anything here?

Sorry if these are not good questions, this is my first time dealing with this

Leo
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 28, 2020

@Matthew Zdonczik 

Have you changed field name in below line?

def cfField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Custom Field Name")

Also have you changed valid Priority name(s) in if..else part?

I did write this script to deal with number field only also I'm not considering issuetype here

please verify events and project(s) mapped with this listener and pass me error log if you see any 

Matthew Zdonczik January 29, 2020

Hi @Leo

Yes, I have changed the "Custom Field Name" to the name of the custom field I created and it is using numbers as the values.

I currently have it mapped to 1 project that I am testing it out on and have it mapped to "All Issue Events"

Is there something else I may be doing wrong?

For Priority levels, should I be using the actual string of the Priority? (minor, moderate, etc.) Or should I use the id value?

Leo
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 29, 2020

@Matthew Zdonczik 

You'll have to use actual string name/value of priority

Others are looking good to me

Matthew Zdonczik January 29, 2020

@Leo 

I tried changing the Prioirty values to the needed strings and it still isn't working properly. 

Is it possible that I need to import anything else?

Leo
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 29, 2020

@Matthew Zdonczik,

this is a working script for me but with string type, I might have made mistake with condition, can you try anyone from below and see if that works

def value = issue.getCustomFieldValue(cfField) 
if(value == 1)

 or

def value = issue.getCustomFieldValue(cfField) as String
if(value == "1")
Matthew Zdonczik January 30, 2020

@Leo 

neither of those worked for me either.

Something I should have noticed/mentioned earlier though, I am seeing a warning 

that states to use "something else" (different things on different lines) since v7.0

Are you trying this out on a Jira version pre 7.0?

Specifically:

def user = ComponentAccessor.getJiraAuthenticationContext().getUser() states to use def user = JiraAuthenticationContext.getLoggedInUser()

And

def cfField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("CVSS Score") instead use

def cfField = CustomFieldManager.getCustomFieldObjectByName("CVSS Score")

Playing around with that now to see if I can clean up the errors on my side

Matthew Zdonczik January 31, 2020

@Leo 

I also gave this a shot after searching around:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.index.IssueIndexingService;
import com.atlassian.jira.util.ImportUtils;
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.event.issue.IssueEvent;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueInputParametersImpl;
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption;

def issue = event.issue
def issueManager = ComponentAccessor.getIssueManager()
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def indexingService = ComponentAccessor.getComponent(IssueIndexingService.class)
def priority = issue.getPriority()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject("CVSS Score")
def cFieldValue = issue.getCustomFieldValue(cField)
def value = ((LazyLoadedOption)cField).getValue()

if(value == "1"){
priority.name == 'Minor'
}else if(value == "2"){
priority.name == 'Minor'
}else if(value == "3"){
priority.name == 'Minor'
}else if(value == "4"){
priority.name == 'Moderate'
}else if(value == "5"){
priority.name == 'Moderate'
}else if(value == "6"){
priority.name == 'Moderate'
}else if(value == "7"){
priority.name == 'Major'
}else if(value == "8"){
priority.name == 'Major'
}else if(value == "9"){
priority.name == 'Critical'
}else if(value == "10"){
priority.name == 'Critical'
}else{
priority.name == 'Minor'
}

 

it is throwing me the following error though:

2020-01-30 14:54:26,359 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2020-01-30 14:54:26,359 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: null
java.lang.NullPointerException
 at com.atlassian.jira.issue.IssueImpl.getCustomFieldValue(IssueImpl.java:951)
 at com.atlassian.jira.issue.Issue$getCustomFieldValue$5.call(Unknown Source)
 at Script438.run(Script438.groovy:20)
Leo
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 31, 2020

I guess this should be def cField = customFieldManager.getCustomFieldObject("CVSS Score")

written as def cField = customFieldManager.getCustomFieldObjectByName("CVSS Score")

Suggest an answer

Log in or Sign up to answer