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

Hi,

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
import com.atlassian.jira.security.groups.GroupManager

//Enable Logging
log.setLevel(org.apache.log4j.Level.DEBUG)

// 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()
log.info("inside if")
GroupSize = groupManager.getUsersInGroup('<group1_name>').size()
RandomMember = Rand.nextInt(Max+1)

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

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){
issue.setAssigneeId(Assignee)
}
else{
def AltRandomMember = RandomMember +1
def AltAssignee = groupManager.getUsersInGroup('<group2_name>').getAt(AltRandomMember).getName()
issue.setAssigneeId(AltAssignee)
}
}

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

1 answer

0 votes
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

issue.store is deprecated, it should work, but the best way is to use the IssueService

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 ...

2,794 views 11 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