Automatic assignment of issues based on custom field values using post function


I have written a post function to automatically assignee an issue based on a particular custom field value.

I have two different user groups in Jira. I want to assignee an issue randomly to different user within these groups based on the value of the custom field i.e. ProjectName here.

It is always executing the else part of the if-else-if condition.

Can someone please help me resolve or identify the mistake I am making here.

As custom field I have a single select list. Consider A,B,C,D,E,F,G, etc. as member of this list.

Below is my post function

import com.atlassian.jira.component.ComponentAccessor
import java.util.Random
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.component.ComponentAccessor

//Enable Logging

// Get the current logged in user
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser().getName()
//Get a pointer to the current Issue
def issueManager = ComponentAccessor.getIssueManager()
//Issue issueKey= issue
//def id=issueKey.getId()

// Decide the group based on product name value 
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject('<customfield_id>')
def customFieldValue = issue.getCustomFieldValue(customField)
def groupManager = ComponentAccessor.getGroupManager()
def GroupSize
// Pick a Random number to determine the member of the Group to assign the issue to
Random Rand = new Random()
int Max
def RandomMember, Assignee
if (customFieldValue == 'A' || customFieldValue == 'B' || customFieldValue == 'C' || customFieldValue == 'D'){
// Work out the size of the Group
//groupManager = ComponentAccessor.getGroupManager()"inside if")
GroupSize = groupManager.getUsersInGroup('<group1_name>').size()
RandomMember = Rand.nextInt(Max+1)

Assignee = groupManager.getUsersInGroup('<group1_name>').getAt(RandomMember).getName()
if (Assignee != user){
def AltRandomMember = RandomMember +1
def AltAssignee = groupManager.getUsersInGroup('<group1_name>').getAt(AltRandomMember).getName()

else if(customFieldValue == "E" || customFieldValue == "F" || customFieldValue == "G"){
//groupManager = ComponentAccessor.getGroupManager()
GroupSize = groupManager.getUsersInGroup('<group2_name>').size()
RandomMember = Rand.nextInt(Max+1)

Assignee = groupManager.getUsersInGroup('<group2_name>').getAt(RandomMember).getName()
if(Assignee !=user){
def AltRandomMember = RandomMember +1
def AltAssignee = groupManager.getUsersInGroup('<group2_name>').getAt(AltRandomMember).getName()

else{"Inside last else")
def usermanager = (UserManager) ComponentAccessor.getUserManager()
ApplicationUser usera = usermanager.getUserByName('<username>')
MutableIssue issue = issue

1 answer

0 vote
Gaston Valente Community Champion Jul 19, 2017

I think that this returns an Object that you need to cast to Option:

def customFieldValue = issue.getCustomFieldValue(customField)

Gaston Valente Community Champion Jul 19, 2017 is deprecated, it should work, but the best way is to use the IssueService

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
Julia Dillon
Posted Tuesday in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

514 views 1 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