Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Can't make this groovy expression work (for conditional selecting assignee)

Markos Stefanou March 1, 2018

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.MutableIssue

String user;

def cfManager = ComponentAccessor.getCustomFieldManager();

def cf = cfManager.getCustomFieldObjectByName("Amount");

switch(issue.getCustomFieldValue(cf) as String)

{

case (int)cfValue("Amount") < 500: user = "alfauser";break;

case (int)cfValue("Amount") < 1000: user = "betauser";break;

case (int)cfValue("Amount") < 2000: user = "gammauser";break;

case "4": user = "username2";break;

case "5": user = "username3";break;

}

issue.setAssignee(ComponentAccessor.getUserManager().getUserByKey(user))

image.png

 

 

2 answers

1 accepted

1 vote
Answer accepted
Gezim Shehu [Communardo]
Community Champion
March 7, 2018

Here is the script

 

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.security.roles.ProjectRole
import com.atlassian.jira.security.roles.ProjectRoleActors
import com.atlassian.jira.security.roles.ProjectRoleManager

ComponentManager componentManager = ComponentManager.getInstance()
ProjectRoleManager projectRoleManager = ComponentManager.getComponentInstanceOfType(ProjectRoleManager.class) as ProjectRoleManager

def String user
def project = issue.getProjectObject()
def int customFieldToCheck = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName('Amount').getValue(issue)

//set all roles
ProjectRole role1 = projectRoleManager.getProjectRole("Administrators")
ProjectRole role2 = projectRoleManager.getProjectRole("Developers")
ProjectRole role3 = projectRoleManager.getProjectRole("Test Leader")

switch(customFieldToCheck){
case 0..500:
//user = 'user1'
ProjectRoleActors actors = projectRoleManager.getProjectRoleActors(role1, project) // replace role
user = actors.getUsers().toList()?.first()?.name //have to specify first, since actors is an array
break
case 501..1000:
ProjectRoleActors actors = projectRoleManager.getProjectRoleActors(role1, project) // replace role
user = actors.getUsers().toList()?.first()?.name //have to specify first, since actors is an array
break
case 1001..2000:
ProjectRoleActors actors = projectRoleManager.getProjectRoleActors(role1, project) // replace role
user = actors.getUsers().toList()?.first()?.name //have to specify first, since actors is an array
break
default:
break
}

if(user){
issue.setAssigneeId(user)
}
Markos Stefanou March 7, 2018

Thank you! 

This should work perfectly :)

Gezim Shehu [Communardo]
Community Champion
March 7, 2018

You're welcome

1 vote
Gezim Shehu [Communardo]
Community Champion
March 5, 2018

I'm confused.

Where exactly is cfValue declared?

Markos Stefanou March 6, 2018

To my knowledge, cfValues is a built in operation, is it not?

Gezim Shehu [Communardo]
Community Champion
March 6, 2018

it's built in , but not for custom script.

Are you using this a custom script post-function?

Markos Stefanou March 6, 2018

Yep.

How am I supposed to declare it? let's see if that's the problem here

Gezim Shehu [Communardo]
Community Champion
March 6, 2018

Could you please exactly specify what you're trying to do and I can help with the script.


Also define the field type

Markos Stefanou March 6, 2018

My goal is to set the assignee depending the value of a field.

Practically, if you have a $500 offer, it requires approval from a line manager, whereas when you have a $10,000 it requires approval from the COO.

I'm trying to set the assignee to be the role member of the COO role if the amount is above 10k. 

I have the numeric field named "Amount" that records the amount that the offer is about.

Gezim Shehu [Communardo]
Community Champion
March 6, 2018

This works. Just get the issue variable and add imports for component accessor.

Also add more cases if desired

 

def String user

def int customFieldToCheck = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName('Amount').getValue(issue)

switch(customFieldToCheck){
case 0..500:
user = 'user1'
break
case 501..1000:
user = 'user2'
break
case 1001..2000:
user = 'user3'
break
default:
break
}

if(user){
issue.setAssigneeId(user)
}
Markos Stefanou March 7, 2018

That's great! TY!

If I wanted to assign it to role members instead of specific users, is there a way to address it? for example MemberOfRole('Accounting') ?

Gezim Shehu [Communardo]
Community Champion
March 7, 2018

Np (Y)

How would you prioritize in case of multiple members? Would it be the first random ?

Markos Stefanou March 7, 2018

It would be a single member in each role so I guess it wouldn't matter.

For example the last role member would be the COO, whereas the previous one would be a Product Manager. Let's just name them Role1, Role2, Role3

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events