Add users to multiuser field in ScriptRunner Behaviour

Jason Adam February 18, 2018

I have the following ScriptRunner behaviour script for our "Required Reviewers" multiuser custom field:

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.UserPropertyManager
import com.atlassian.jira.issue.IssueManager

class SetRequiredReviewers extends com.onresolve.jira.groovy.user.FieldBehaviours
{
 def run()
 {
  IssueManager issueManager = ComponentAccessor.getIssueManager();
  def mutableIssue = issueManager.getIssueObject(underlyingIssue.id)
  def groupManager = ComponentAccessor.getGroupManager()
  def customFieldManager = ComponentAccessor.getCustomFieldManager()
  List<ApplicationUser> reviewMembers = groupManager.getUsersInGroup("Review Board Member")
  def reqRevField = customFieldManager.getCustomFieldObjectByName("Required Reviewers")
  mutableIssue.setCustomFieldValue(reqRevField, reviewMembers)

 }
}

However, it never populates, even though if I do some log debug statements, the users I currently have in the Review Board Member group are being found.  There's no errors, either.

I also tried adding just a list of user names, but that didn't work, either.

What am I doing wrong here?

 

Thanks!

2 answers

1 vote
Alexey Matveev
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.
February 18, 2018

Try like this

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.UserPropertyManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.event.type.EventDispatchOption

class SetRequiredReviewers extends com.onresolve.jira.groovy.user.FieldBehaviours
{
 def run()
 {
  IssueManager issueManager = ComponentAccessor.getIssueManager();
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
  def mutableIssue = issueManager.getIssueObject(underlyingIssue.id)
  def groupManager = ComponentAccessor.getGroupManager()
  def customFieldManager = ComponentAccessor.getCustomFieldManager()
  List<ApplicationUser> reviewMembers = groupManager.getUsersInGroup("Review Board Member")
  def reqRevField = customFieldManager.getCustomFieldObjectByName("Required Reviewers")
  mutableIssue.setCustomFieldValue(reqRevField, reviewMembers)
ComponentAccessor.getIssueManager().updateIssue(user, mutableIssue, EventDispatchOption.ISSUE_UPDATED, false)
 }
}

Jason Adam February 18, 2018

Thanks for the response!

I'm trying to populate the field on screen launch.  Basically pre-populate the field for the user with default users pulled from the Review Board Member group.  So while that works, I have to go back into that state transition to see the update, and then continue with the flow.

If there was a way to set the default users in a multiuser select field to a group, instead of having to add individual users as default, that would be ideal.  Unfortunately I don't see a way to do that so I was using a behaviour.  Is there a better way?

Alexey Matveev
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.
February 19, 2018

You are not using behaviours the right way. You should set values with setFormValue method. 

https://scriptrunner.adaptavist.com/latest/jira/behaviours-api-quickref.html

Jason Adam February 19, 2018

I had tried that previously (all my other behaviors use the normal api and work fine), but all I get in the multiuser field is [object Object] when call reqField.setFormValue(reviewMembers). 

So perhaps I'm just not passing the correct list of values.  I'll mess around with it more.  Thanks!

Jason Adam February 19, 2018

Ah yup that was my problem.  I was passing in the list of ApplicationUser and apparently via a Behaviour you have to pass in an array of user name Strings.  Got it!  THank you for the help.

Alexey Matveev
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.
February 19, 2018

I am glad you solved the problem

scott_boisvert
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 27, 2021

@Jason Adam Can you share your script for updating the multi user select field. I can't seem to get this to work. 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue

def usersField = ComponentAccessor.customFieldManager.customFieldObjects.findByName("User(s)")
String[] users = ["boisvers"]
usersField.setFormValue(users)

scott_boisvert
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 28, 2021

Figured it out, you have to use getFieldByName for service desk/management forms:

def usersField = getFieldByName("User(s)")
usersField.setFormValue(["boisvers"])
0 votes
Daphnis Hessling December 5, 2019

Hi Jason,

could you please tell me how exactly did you solve this problem?

I am trying to achieve a similar thing. I want to pre populate a mult userpicker field with values from the same field of the parent issue. I tried a lot of approaches with arrays but just couldn't get it to work...

Here is my script so far:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager

def UserManager = ComponentAccessor.getUserManager()
def parent = getFieldById("parentIssueId")
Long parentIssueId = parent.getFormValue() as Long
def issueManager = ComponentAccessor.getIssueManager()
def parentIssue = issueManager.getIssueObject(parentIssueId)

//Parent
def teilnehmerField = customFieldManager.getCustomFieldObject(11400)
def parentValue = parentIssue.getCustomFieldValue(teilnehmerField).toString()

//Child
def teilnehmer = getFieldById("customfield_11400")
teilnehmer.setFormValue(parentValue)

I am only getting "[hessling(hessling)" pre populated in the multi user picker field, but not the usernames...

Many thanks in advance

Daphnis

Suggest an answer

Log in or Sign up to answer