[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 Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Posted 9 hours ago in Confluence

Calling all marketing teams who use Confluence - we want to hear from you!

Hi Community! me again 🙂 If you’re a marketing team using Confluence, we want to hear your story! How did you start using Confluence? What are your use cases? What have been some of the benefits?...

41 views 1 2
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