Jira automatic assignment based on custom field value

Any quick tips? I know the groovy script runner is out there -- but my version doesn't have something for just assigning issues (though I see that some of the scripts, like the fast-track workflow listener, allows users to also assign an issue..)

Basically, when issues are normally created in this particular queue, it automatically gets assigned to the project lead.

I want to maintain this functionality, but I want the issue to be assigned to another user based on a value in a select list custom field.

Help much appreciated. I'm assuming some sort of listener is needed..?

3 answers

1 accepted

This widget could not be displayed.

Hi Bryan,

Please give a try to this third plugin known as User Picker From Project Role - Issue Alternative Assignee.

Good luck

thanks -- looks promising. :)

This widget could not be displayed.

Just to rule it out - I take it components aren't suitable? (i.e. assign to component lead)

Unfortunately, that approach wont work due to how our solution is set up.

This widget could not be displayed.

In custom script listener hooked on Create event you can use any of the issue fields, built-in or custom, to do what ever you want:

like assign to hardcoded person based on priority, issue type, etc.

Here an example of complete Listener class file you can modify for your task.

Many people are asking how to start with custom listeners, how to name a class, where to put a file, etc.

I hope this sample will provide some guidance. Just copy it to a file <class name>.groovy and copy to a place where groovy runner can find it (check out this answer)

import java.util.*
import java.sql.Timestamp
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.*
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.issue.link.*
import com.atlassian.jira.event.issue.AbstractIssueEventListener
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.CustomFieldManager
import org.apache.log4j.Category
import com.atlassian.crowd.embedded.api.User
import com.atlassian.plugin.PluginAccessor
import com.atlassian.jira.bc.project.ProjectService
import com.atlassian.jira.user.util.UserUtil

//rename your listener class as needed 
class AutoIssueAssignerListener extends AbstractIssueEventListener {
    Category log = Category.getInstance(LoggerService.class)
 
    @Override
    void workflowEvent(IssueEvent event) {
        // only one central way...
        this.customEvent(event)
    }
 

	ComponentManager componentManager = ComponentManager.getInstance()
	
    @Override
    void customEvent(IssueEvent event) {
  
	// set explicit to debug
    log.setLevel(org.apache.log4j.Level.DEBUG) // remove for production
	log.debug "Groovy Service Class name: ${this.getClass().getName()}"
    log.debug "Event: \"${ComponentManager.instance.eventTypeManager.eventTypesMap[event.getEventTypeId()].name}\" fired for ${event.issue}"
 
        // Here you should put any restrictions for this task like
        // only for special issue types or similar
	Issue issue = event.issue
    User user = issue.getAssigneeUser() 
	
	//
	if (issue.isSubTask()) { 
		return
	}
		
	def customFieldManager = ComponentManager.getInstance().getCustomFieldManager()
	def cfCategory = customFieldManager.getCustomFieldObjectByName("Category") //use your custom field name here 
	def category = issue.getCustomFieldValue(cfCategory)
	
	def userToReassign = issue.getAssignee()
	UserUtil userUtil = componentManager.getUserUtil()
	
	switch (category) {
	  case "One": 
userToReassign = userUtil.getUserObject("jsmith")
break case "Two":
userToReassign = issue.getProjectObject().getLead()
break } issue.setAssignee(userToReassign) issue.store() } log.debug "Listener ${this.getClass().getName()} completed" } }

It is not tested. Just removed my stuff from working listener and threw in few lines of code that might be relevant to your task

Updated my answer with code sample

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted yesterday in Jira

What modern development practices are at the heart of how your team delivers software?

Hey Community mates! Claire here from the Software Product Marketing team. We all know software development changes rapidly, and it's often tough to keep up. But from our research, we've found the h...

39 views 0 1
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