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

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
Accepted answer

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)

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

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

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
Community showcase
Posted Tuesday in Statuspage

Introducing Statuspage Getting Started guides! First up: What is Statuspage?

Over the next several weeks we'll be sharing some of our Getting Started guides here in the community. Throughout this series of posts, we'd love to hear from customers and non-customers ab...

26 views 2 1
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you