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

0 votes

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

0 votes

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.

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 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,849 views 12 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