[GROOVY] Set priority script

Hello people smile,

 I have a problem with my new script. How it works. In our project we have 2 single-choice fields (Urgency, Impact) and this script depending on their values sets priority of ticket. Problem is that...it doesn't change priority sad. Can somebody look into it and tell me where i made a mistake please?

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.config.PriorityManager
import com.atlassian.jira.issue.priority.Priority
import com.atlassian.jira.config.ConstantsManager
import org.ofbiz.core.entity.GenericValue

MutableIssue currentIssue = issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def urgency = customFieldManager.getCustomFieldObjectByName("Urgency")
def impact = customFieldManager.getCustomFieldObjectByName("Impact")
def uValue = urgency.getValue(currentIssue).toString()
def iValue = impact.getValue(currentIssue).toString()
int uNumb, iNumb
switch (uValue) {
   case "Cannot Work":
     uNumb = 1
     break;
 
   case "Obstruction of Work":
     uNumb = 2
     break;
    
   case "Functionality Problem":
     uNumb = 3
     break;
    
   default:
     System.out.println();
}
switch (iValue) {
   case "Company Wide":
     iNumb = 1
     break;
 
   case "More then one project":
     iNumb = 2
     break;
    
   case "Single project":
     iNumb = 3
     break;
   case "Individual":
     iNumb = 4
     break;
    
   default:
     System.out.println();
}
int pValue = iNumb + uNumb
def priority = " "
if(pValue >= 1 && pValue <= 2)
	priority = "Critical"
else if(pValue >= 3 && pValue <= 4)
    priority = "Major"
else if(pValue >= 6 && pValue <=8)
    priority = "Normal"
else
    priority = "Minor"
def cm = ComponentAccessor.getConstantsManager()
def priorities = cm.getPriorities()
priorities.each { prio ->
    if(prio.name == priority)
    	currentIssue.setPriorityObject(prio)
}

It is set into postfunctions on initial "Create" transition as last one of them, and it shows there is no failures.

Thanks in advance,
Cheers,
Maciej O. 

1 answer

1 accepted

This widget could not be displayed.
Vasiliy Zverev Community Champion Mar 15, 2017

An build-in postfucntion "Save to DB" should be added on create transiton after custom script.

I also refactored your code

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

MutableIssue currentIssue = issue
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

int num;

switch (customFieldManager.getCustomFieldObjectByName("Urgency").getValue(currentIssue).toString()) {
    case "Cannot Work":
        num = 1;
        break;

    case "Obstruction of Work":
        num = 2
        break;

    case "Functionality Problem":
        num = 3
        break;

    default:
        System.out.println();
}

switch (customFieldManager.getCustomFieldObjectByName("Impact").getValue(currentIssue).toString()) {
    case "Company Wide":
        num += 1
        break;

    case "More then one project":
        num += 2
        break;

    case "Single project":
        num += 3
        break;
    case "Individual":
        num += 4
        break;

    default:
        System.out.println();
}

String priority;
switch (num){
    case 1: case 2: priority = "Critical"; break;
    case 3: case 4: priority = "Major"; break;
    case 6: case 7: case 8: priority = "Normal"; break;
    default: priority = "Minor"
}

for(Priority priority1: ComponentAccessor.getConstantsManager().getPriorityObjects()){
    if(priority1.getName().equals(priority)){
        currentIssue.setPriorityObject(issue)
        break;
    }
}

Thank you very much it works!

For me it does not work: image.png

i guess it hast to be 

"currentIssue.setPriorityObject(priority)"

and not

"currentIssue.setPriorityObject(issue)".

However, this is only a guess and it isn't working for me either.

For me works:
currentIssue.setPriorityId(ComponentAccessor.getConstantsManager().getPriorities().find{it.getName().equals("Name of the priority")}.getId());

// Update Issue
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted yesterday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

72 views 1 0
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