Assign Issue based on a Custom Field Value

Deleted user August 5, 2013

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

1 vote
Answer accepted
Henning Tietgens
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.
August 5, 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.


Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 5, 2013

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

Deleted user August 5, 2013

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
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.
August 5, 2013

Now remove the line

void customEvent(IssueEvent event) {

and the closing } and try again.

Deleted user August 5, 2013

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()

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 5, 2013

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

Deleted user August 5, 2013

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

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 6, 2013

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.

Deleted user August 6, 2013

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

Henning Tietgens
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.
August 6, 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
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.
August 6, 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)

Deleted user August 6, 2013

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

1 vote
RambanamP
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.
August 5, 2013

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

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 5, 2013

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.

RambanamP
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.
August 6, 2013

@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

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 6, 2013

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.

RambanamP
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.
August 6, 2013

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

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 6, 2013

1.3.5 here.

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

0 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 5, 2013

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

Deleted user August 5, 2013

Yeah, exactly.

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 5, 2013

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?

Deleted user August 5, 2013

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

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 5, 2013

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.

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 5, 2013

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

Deleted user August 5, 2013

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
TAGS
AUG Leaders

Atlassian Community Events