Assign Issue based on a Custom Field Value

I would like to add a custom value field (a dropdown select box) to show the different teams available, so that the user can select from a controlled list of teams (rather then the Assign box which shows all the users). I then want this to assign the issue to that team.

I have tried using a groovy script as part of the workflow to reassign it to the specific team, but I wasn't able to get it working.

So on my create screen I have a select box with the following;

Network Team
Security Team
Email Team

Scenario example, a user is raising a network problem they select Network Team and the issue is then assigned to the user "Network Team"

3 answers

1 accepted

This widget could not be displayed.
Henning Tietgens Community Champion Aug 05, 2013

First, your code is a listener class, not a postfunction script. Try to use only the code within the method customEvent(IssueEvent event) as your post function.

Second, category will be an Option object if your custom field is a select list. You have to use

def category = issue?.getCustomFieldValue(cfCategory)?.value

to get the selected value.


Oh, thanks Henning, I'd completely missed that!

So this is my new script that I have, but it's still not working, still going to the default;

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
 

    void customEvent(IssueEvent event) {

    Issue issue = event.issue
    User user = issue.getAssigneeUser() 
 
    if (issue.isSubTask()) { 
        return
    }
         
    def customFieldManager = ComponentManager.getInstance().getCustomFieldManager()
    def cfCategory = customFieldManager.getCustomFieldObjectByName("Team Involved") //use your custom field name here 
    def category = issue?.getCustomFieldValue(cfCategory)?.value
     
    def userToReassign = issue.getAssignee()
    UserUtil userUtil = componentManager.getUserUtil()
     
    switch (category) {
      case "Desktop": <br>userToReassign = userUtil.getUserObject("DesktopSupport")<br>break
      case "Network": <br>userToReassign = userUtil.getUserObject("NetworkSupport")<br>break
    }
    issue.setAssignee(userToReassign)
    issue.store()  
     
    }

Henning Tietgens Community Champion Aug 05, 2013

Now remove the line

void customEvent(IssueEvent event) {

and the closing } and try again.

I'm now running this, with no luck

User user = issue.getAssigneeUser() 
if (issue.isSubTask()) { 
return
}
     
def customFieldManager = ComponentManager.getInstance().getCustomFieldManager()
def cfCategory = customFieldManager.getCustomFieldObjectByName("Team Involved")
def category = issue?.getCustomFieldValue(cfCategory)?.value
     
def userToReassign = issue.getAssignee()
UserUtil userUtil = componentManager.getUserUtil()
     
switch (category) {
case "Desktop": <br>userToReassign = userUtil.getUserObject("Desktop Support")<br>break
case "Network": <br>userToReassign = userUtil.getUserObject("Network Support")<br>break
}
issue.setAssignee(userToReassign)
issue.store()

Where in the order of post functions are you doing this?

It's last in the list of post functions on the Create link between Create and Open processes.

Oh, the create transition. Ugh. That thing seems to do horrid things to post-functions sometimes.

Two quick things to try (separately):

1. Remove all the "default assignee" stuff - configure your project so that issues are unassigned by default, rather than project or component leads

2. Move the function up above "re-index" in that order.

Tried them, but it's now just raising as 'unassigned'

Henning Tietgens Community Champion Aug 06, 2013

Try this

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.user.util.UserUtil
import org.apache.log4j.Level
import org.apache.log4j.Logger

MutableIssue issue
ComponentManager componentManager = ComponentManager.instance
Logger log

log.setLevel(Level.DEBUG)

log.debug "Starting postfunction..."
if (issue.isSubTask()) {
    log.debug "Issue is subtask, leaving."
    return
}

def customFieldManager = componentManager.getCustomFieldManager()
def cfCategory = customFieldManager.getCustomFieldObjectByName("Team Involved")
log.debug "Found customfield ${cfCategory}."
def category = (issue?.getCustomFieldValue(cfCategory) as Option)?.value
log.debug "Got category ${category}."

def userToReassign = issue.getAssignee()
log.debug "Got assignee ${userToReassign}."
UserUtil userUtil = componentManager.getUserUtil()

switch (category) {
    case "Desktop": 
        log.debug "Getting user for Desktop category."
        userToReassign = userUtil.getUserObject("Desktop Support")
        break
    case "Network":
        log.debug "Getting user for Network category."
        userToReassign = userUtil.getUserObject("Network Support")
        break
}
log.debug "Setting user to ${userToReassign}."
issue.setAssignee(userToReassign)

before the "Creates the issue originally". If it doesn't work please post the corresponding log.

Henning Tietgens Community Champion Aug 06, 2013

Uups, to much.. The first lines are only for the IDE, so they have to be removed, resulting in this.

import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.user.util.UserUtil
import org.apache.log4j.Level

log.setLevel(Level.DEBUG)

log.debug "Starting postfunction..."
if (issue.isSubTask()) {
    log.debug "Issue is subtask, leaving."
    return
}

def customFieldManager = componentManager.getCustomFieldManager()
def cfCategory = customFieldManager.getCustomFieldObjectByName("Team Involved")
log.debug "Found customfield ${cfCategory}."
def category = (issue?.getCustomFieldValue(cfCategory) as Option)?.value
log.debug "Got category ${category}."

def userToReassign = issue.getAssignee()
log.debug "Got assignee ${userToReassign}."
UserUtil userUtil = componentManager.getUserUtil()

switch (category) {
    case "Desktop":
        log.debug "Getting user for Desktop category."
        userToReassign = userUtil.getUserObject("Desktop Support")
        break
    case "Network":
        log.debug "Getting user for Network category."
        userToReassign = userUtil.getUserObject("Network Support")
        break
}
log.debug "Setting user to ${userToReassign}."
issue.setAssignee(userToReassign)

THANK YOU!!!

I had to change the assignee's from "Desktop Support" to "DesktopSupport" for it to pick it up, but it has now.


Perfect :)

This widget could not be displayed.

you can you "Copy Value From Other Field Post-Function" from JSU plugin

check this

https://jsutil.atlassian.net/wiki/display/JSUTIL/JIRA+Suite+Utilities+Workflow+Post-Functions

Erm, sorry, no, that's wrong.

That function can only copy between fields of compatible types. e.g. text to text, date to date etc. The request here is to copy an <option> to a <user>, so that's never going to work.

@Nic,

you are wrong!!

it is worked for me, just now i created one select list with by hot coding user names as options and configured "Copy Value From Other Field Post-Function" to copy the selct list field to assignee, it worked like Charm!!

if you want you can check it out ! i tested it on JSU 1.3.5 version

Well, it fails miserably in both Jira installs I've tried it in. One of them throws a cast exception when the function executes and the other lets you do it, but you end up with an empty field and an error in the log when the field is rendered.

So, it might work. I've not seen it do so.

which version of JSU you have tested it on.

i have check the source code and it supporting to copy value from select list to assignee

check the setFieldValue method here

https://github.com/atlassian/jira-suite-utilities/blob/master/src/main/java/com/googlecode/jsu/util/WorkflowUtils.java

1.3.5 here.

So it works for some installations and not others. Worth a try.

This widget could not be displayed.

To clarify - is "Network Team" a dummy user as well as a line in your selection list?

Ok, that's a good plan, and I think the groovy script is by far the best approach.

I'd say we need to see what code you've got so far, know where it is running (post function?) and details on what is not working with it?

It's set as a post function in the workflow, and the problem is that it's still assigning the issue to the project lead (me) and not the team selected, I've put the script over at pastebin as it was too big to put here

http://pastebin.com/gPYg6U84

Ok, your code looks fine to me, although, I'm not 100% sure of this line:

userUtil.getUserObject("Network Support")

Is that definitely returning the user object for the user with that name?

The other question is where the post-function is being run - what is the order of the 5 off-the-shelf postfunctions and this one? I've got a feeling that if it's running too early in that order, then the assignee you set will be overwritten later in the process.

I've just dug getUserByName("Dave") out of some of my code, but I think that might be wrong as well, depending on your Jira version.

I'd have a rummage through https://docs.atlassian.com/jira/latest/com/atlassian/jira/user/util/UserUtil.html

I don't know if that line does pull it across, I think this might be my problem.

I've got the line at the bottom of the post functions.

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

155 views 2 0
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