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 Sign up to answer
Community showcase
Published Nov 27, 2018 in Portfolio for Jira

Introducing a new planning experience in Portfolio for Jira (Server/DC)

In the past, Portfolio for Jira required a high degree of detail–foresight that was unrealistic for many businesses to   have–in   order to produce a reliable long-term roadmap. We're tur...

2,729 views 17 21
Read article

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