It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Assignee field not updated on form when using Behavior Script

Jodi Gornick May 02, 2017

I am using a behavior to set the assignee field to the current user if it is not set on transition screens.  Per logging, the value is retreived and set, however the value is not reflected on the form.

Any thoughts?

BEHAVIOR SCRIPT (on Assignee field):

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import static com.atlassian.jira.issue.IssueFieldConstants.*
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours
log.debug("-- Starting behavior script to set assignee when not provided")
log.debug("Form Value " + getFieldByName("Assignee").getValue())

if (getFieldByName("Assignee").getValue() == null) {
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser().name
log.debug("Setting assignee in TransitionForm to currentUser: " + currentUser)

getFieldByName("Assignee").setFormValue(currentUser)
log.debug("Form Value after setting " + getFieldByName("Assignee").getValue())

}

 

DEBUG:

2017-05-02 [c.o.j.groovy.user.FieldBehaviours] -- Starting behavior script to set assignee when not provided
2017-05-02 [c.o.j.groovy.user.FieldBehaviours] Form Value null
2017-05-02 [c.o.j.groovy.user.FieldBehaviours] Setting assignee in TransitionForm to currentUser: jodi.avery@company.com
2017-05-02 [c.o.j.groovy.user.FieldBehaviours] Form Value after setting jodi.avery@company.com

2 answers

1 accepted

0 votes
Answer accepted
Jonny Carter May 10, 2017

You're really close. Basically, you need to do two things:

1) Use the IssueFieldConstants that you imported by calling to the constant ASSIGNEE, rather than "Assignee", which isn't correct.

2) Account for the case where assignee could equal -1, which is the value for the default "Automatic" assignee.

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

import static com.atlassian.jira.issue.IssueFieldConstants.ASSIGNEE

@BaseScript FieldBehaviours fieldBehaviours
log.debug("-- Starting behavior script to set assignee when not provided")
def assigneeField = getFieldById(ASSIGNEE)
log.debug("Form Value " + assigneeField.getValue())
if (assigneeField.getValue() == "-1" || assigneeField.getValue() == null) {
    def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser().name
    log.debug("Setting assignee in TransitionForm to currentUser: " + currentUser)
    assigneeField.setFormValue(currentUser)
    log.debug("Form Value after setting " + assigneeField.getValue())
}
CLINTON E. SMITH Mar 12, 2018

Jonny,

This thread is kind of old, but I see that 'Automatic' is actually a value of -1.  Is there a value for 'Unassigned' as well, should we have a need to actually default one or both of these selections using Behaviours?

0 votes
Thanos Batagiannis [Adaptavist] Community Leader May 10, 2017

Hi Jodi,

Try something like 

getFieldById(ASSIGNEE).setFormValue("anuser")

This will set the assignee to the user with username anuser.

Actually try to use the id instead of the name for system fields. A good way to find the id for a system field is from IssueFieldConstants

And if you use an IDEAto write your scripts you can use it like 

import com.onresolve.jira.groovy.user.FieldBehaviours
import static com.atlassian.jira.issue.IssueFieldConstants.*

import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

getFieldById(ASSIGNEE).setFormValue("anuser")

Hope that helps, 

Thanos

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Next-gen

Introducing subtasks for breaking down work in next-gen projects

Teams break work down in order to help simplify complex tasks. This is often done iteratively, with tasks being broken down into smaller tasks and so on until the work is accurately captured in well-...

988 views 12 15
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you