[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
Alexey Matveev Community Champion Dec 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 Community Champion Dec 20, 2017

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

Alexey Matveev Community Champion Dec 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 Join to answer
Community showcase
Emilee Spencer
Published Friday in Marketplace Apps

Marketplace Spotlight: DeepAffects

Hello Atlassian Community! My name is Emilee, and I’m a Product Marketing Manager for the Marketplace team. Starting with this post, I'm kicking off a monthly series of Spotlights to highlight Ma...

50 views 0 3
Read article

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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot