Change Priority when issue type changes

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

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.

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;
    }
}

Thanks Rei. The problem is solved now.

 

  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!

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

364 views 0 6
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