[GROOVY] 1)change priority based on custom field 2)update issue -- >Works for JIRA Server 7.4.x

Sven Koch December 20, 2017

EDIT: Thanks toAlexey Matveev the script is now working and all questions are solved

Below the edited script which works for JIRA 7.4.x Server

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.priority.Priority
import org.apache.log4j.Logger
import org.apache.log4j.Level
import com.atlassian.jira.event.type.EventDispatchOption

 

def currentIssue = (MutableIssue) issue
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

 

//switch depending on the values of custom field and set priority accordingly
switch (customFieldManager.getCustomFieldObjectByName("Name of custom field").getValue(currentIssue).toString())

{
case "1. First value custom field":
currentIssue.setPriorityId(ComponentAccessor.getConstantsManager().getPriorities().find{it.getName().equals("Here goes the first priority name")}.getId());
break;

case "2. Second value custom field":
currentIssue.setPriorityId(ComponentAccessor.getConstantsManager().getPriorities().find{it.getName().equals("Here goes another priority name")}.getId());
break;

case "3. Third value custom field":
currentIssue.setPriorityId(ComponentAccessor.getConstantsManager().getPriorities().find{it.getName().equals("Here goes the last priority name")}.getId());
break;

default:
break;
}

// Update Issue
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)

 

1 answer

1 accepted

1 vote
Answer accepted
Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 20, 2017

Hello,

1. You can find IDs of priorities if you go to cog item->issues->priorities and then hover on the edit item.

2. If you want to save the issue then add your post-function first in the list of post functions or add to the script in the first line

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

and add to the end of your script

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)
Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 20, 2017

You updated the script. So what are now your questions? Are they the same?

Sven Koch December 20, 2017

Hi Alexey, 
thanks for your answer - problem 2 saving the issue is solved!!!

However, I was still not able to find the ID of priority. 
We use JIRA Server 7.4.x 
1. I go to issues --> Issue attributes -->Priorities
2. The list of the priorities is displayed

Problem:
When I hover over the link "Edit" in the column Actions there is no priority displayed
When I choose edit I can neither see nor change priority

Am I missing the meaning of " cog item"?

Thanks,
Sven

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 21, 2017

Do it another way 

change 

currentIssue.setPriorityId('3');

to

currentIssue.setPriorityId(ComponentAccessor.getConstantsManager().getPriorities().find{it.getName().equals("High")}.getId()))

Instead "High" you put your required priority

Suggest an answer

Log in or Sign up to answer