Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,359,799
Community Members
 
Community Events
168
Community Groups

Add users to multiuser field in ScriptRunner Behaviour

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

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

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?

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

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!

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.

I am glad you solved the problem

@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 Apr 28, 2021

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

def usersField = getFieldByName("User(s)")
usersField.setFormValue(["boisvers"])

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
TAGS
Community showcase
Published in Jira

Online AMA this week: Your project management questions answered by Jira Design Lead James Rotanson

We know that great teams require amazing project management chops. It's no surprise that great teams who use Jira have strong project managers, effective workflows, and secrets that bring planning ...

168 views 1 6
Read article

Atlassian Community Events