[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

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 Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,042 views 13 18
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
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