Change Priority when issue type changes

sakshi Mittal May 4, 2016

I am writing a custom script on the event "Issue Moved" to change the priority to "Critical".

Basically I want that whenever an "Incident" is moved to issue type "HotFIx" ; the priority gets changed to "Critical".

My code is as below. But doesn't seem to be working. There are syntax errors for eg:

No Such Property : issueTypeObject for class : java.lang.Class

Can you suggest?

import com.atlassian.jira.issue.Issue
if(Issue.issueTypeObject.name == "HotFix")
{Issue.setPriority() = "Critical"}

2 answers

1 vote
sakshi Mittal May 4, 2016

Does not seem to be working for me. When I am trying to convert issue type from Incident with Major Priority to Hotfix issue type the priority remains as Major and does not change to Critical.

But I also noticed that below the script there is a small text that says "Has not Run Yet".

Is there something I am missing.

Rei Song May 4, 2016

It looks like the script didn't get triggered for "Issue Moved" event. I changed the event to "Issue Updated" and modified the code as follows and it works:

 

import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.component.ComponentAccessor;
 
// Retrieve necessary objects
def jac = ComponentAccessor.getJiraAuthenticationContext();
def im = ComponentAccessor.getIssueManager();
def chm = ComponentAccessor.getChangeHistoryManager();
 
// Retrieve the updated issue
MutableIssue currentIssue = (MutableIssue)event.getIssue();
 
// Get the most recent changes
def allChangeItems = chm.getChangeHistories(currentIssue);
def allRecentChanges = allChangeItems[allChangeItems.size() - 1].getChangeItemBeans();

// Go through the recently changed items and check if issuetype is one of them
for (int i = 0; i < allRecentChanges.size(); i++) {
    if (allRecentChanges[i].getField() == "issuetype") {
        // Check if the new issue type is HotFix
        if (allRecentChanges[i].getToString() == "HotFix") {
            // Set priority value
            currentIssue.setPriorityId("2");
             
            // Save issue
            im.updateIssue(jac.getLoggedInUser(), currentIssue, EventDispatchOption.ISSUE_UPDATED, false);
        }
        
        else {
            continue;
        }
    }
    
    else {
        continue;
    }
}
sakshi Mittal May 6, 2016

Thanks Rei. The problem is solved now.

 

0 votes
Rei Song May 4, 2016
  1. I believe you should use issue.getIssueTypeObject().getName() == "HotFix" instead.
  2. You actually need to pass in a value for the setPriority() method, which is an object of Priority type. I found it easier to use the setPriorityId() method instead, where you can just pass in the ID equivalent of the priority as a string. To check this value, go to http://<your JIRA URL>/secure/admin/ViewPriorities.jspa, and try to edit the pertinent priority. You will find its ID value at the end of the URL. 
  3. You should save the issue at the end as well to store the new value to the database.

The actual code would be:

 

import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.component.ComponentAccessor;
 
// Retrieve necessary objects
def jac = ComponentAccessor.getJiraAuthenticationContext();
def im = ComponentAccessor.getIssueManager();
 
// Retrieve the updated issue
MutableIssue currentIssue = (MutableIssue)event.getIssue();
 
if (currentIssue.getIssueTypeObject().getName() == "HotFix") {
	// Set priority value
	currentIssue.setPriorityId("2");
	
	// Save issue
	im.updateIssue(jac.getLoggedInUser(), currentIssue, EventDispatchOption.ISSUE_UPDATED, false);
}

 

In our JIRA instance, the priority ID for "Critical" is "2". Hope that helps!

Steve Letch July 4, 2019

thanks rei, going to give this a go

Steve Letch July 4, 2019

bunch of errors on that one, might have changed over the years

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events