Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

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

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

1 vote

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

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
Posted in Jira Core

How to manage many similar workflows?

I have multiple projects that use variations of the same base workflow. The variations depend on the requirements of the project or issue type. The variations mostly come in the form of new statuses ...

3,894 views 11 5
Join discussion

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