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

Divyank Dubey July 19, 2017

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
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 19, 2017

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

def customFieldValue = issue.getCustomFieldValue(customField)

Gaston Valente
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 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