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

How to set/add to a multiple user picker custom field using Groovy in post function?

Mike Whitlock Dec 15, 2014

Hello,

 

I have been working on this for awhile and can't come up with (or find on after searching for many hours) a way to add to or update a multi user select field using groovy in a post function.  For example, during a post function I need to add 2 users to a custom field that is a user picker (multiple users).  The users have IDs in JIRA of 'rwayne' and 'aluck'. Has anyone done this before and could give me the groovy code to accomplish this?  

 

Thanks for your time and help,

Mike

4 answers

1 accepted

2 votes
Answer accepted
Mike Whitlock Dec 17, 2014

I was able to get this to work now.  Here is the correct code...

 

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.user.util.UserUtil;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.util.IssueChangeHolder

ComponentManager componentManager = ComponentManager.getInstance();
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager();

def userUtil = ComponentAccessor.getUserUtil();
def issueFactory = componentManager.getIssueFactory();
def issueManager = ComponentAccessor.getIssueManager();
def subTaskManager = componentManager.getSubTaskManager();
def indexManager = componentManager.getIndexManager();
import com.atlassian.jira.issue.ModifiedValue

def MutableIssue issue = componentManager.getIssueManager().getIssueObject("ITCMB-116");

CustomField cf = customFieldManager.getCustomFieldObjectByName( "Approvers" );

ApplicationUser user = userUtil.getUserByKey("mwhitlock");
ApplicationUser user2 = userUtil.getUserByKey("tmoody");

List<ApplicationUser> newApprovers = [];
newApprovers.add(user);
newApprovers.add(user2);

issue.setCustomFieldValue(cf, newApprovers);
Object cfVal = issue.getCustomFieldValue(cf);

IssueChangeHolder changeHolder = new DefaultIssueChangeHolder();
cf.updateValue(null, issue, new ModifiedValue("", newApprovers), changeHolder);

issue.store();

0 votes
Mike Whitlock Dec 17, 2014

Since there has no response I will put my code and see if someone can help me... This runs in script runner and gives no error but then when I refresh the issue the custom field that I am trying to populate is still NULL.  But when I check the value of the custom field (the 2nd to last line) it has the correct values that I want.  Can someone tell me why the issue is not updating??

 

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.user.util.UserUtil;
import com.atlassian.jira.user.ApplicationUser;

ComponentManager componentManager = ComponentManager.getInstance();
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager();

def userUtil = ComponentAccessor.getUserUtil();
def issueFactory = componentManager.getIssueFactory();
def issueManager = ComponentAccessor.getIssueManager();
def subTaskManager = componentManager.getSubTaskManager();
def indexManager = componentManager.getIndexManager();

def MutableIssue issue = componentManager.getIssueManager().getIssueObject("ITCMB-116");

CustomField cf = customFieldManager.getCustomFieldObjectByName( "Approvers" );

ApplicationUser user = userUtil.getUserByKey("mwhitlock");
ApplicationUser user2 = userUtil.getUserByKey("tmoody");

List<ApplicationUser> newApprovers = [];
newApprovers.add(user);
newApprovers.add(user2);

issue.setCustomFieldValue(cf, newApprovers);
Object cfVal = issue.getCustomFieldValue(cf); 

issue.store();

Marketa Dvorackova Jul 07, 2015

Hi Mike, I wanted to ask you if you somehow found out what the problem was. I am unfortunately experiencing the same problem. Everything smooth but no result. I am working with groups and multi-group field, but something tells me that the problem is similar. Thank for help if you can.

Mike Whitlock Jul 08, 2015 • edited May 14, 2018

Hi Marketa, This is the code that I use below and it works to add multiple users to a multi-user field...

 

import com.atlassian.jira.ComponentManager 
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.user.util.UserUtil;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder;
import com.atlassian.jira.issue.util.IssueChangeHolder;
import com.atlassian.jira.issue.ModifiedValue;
import com.atlassian.jira.issue.customfields.manager.OptionsManager;
ComponentManager componentManager = ComponentManager.getInstance();
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager();
IssueChangeHolder changeHolder = new DefaultIssueChangeHolder();
def userUtil = ComponentAccessor.getUserUtil();
def issueFactory = componentManager.getIssueFactory();
def issueManager = ComponentAccessor.getIssueManager();
def subTaskManager = componentManager.getSubTaskManager();
def indexManager = componentManager.getIndexManager(); //multi-user custom field to be populated with users who need to approve
CustomField Approvers = customFieldManager.getCustomFieldObjectByName("Approvers"); //cascading select custom field with functional areas
FunctionalArea = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_12986"));
String FA; //get the first value of the cascading select custom field
try{
FA = FunctionalArea.get(null).getGenericValue().value.toString();
}
catch(NullPointerException e)
{
FA = "FA was NULL";
}
List<ApplicationUser> newApprovers = [];
if (FA == "Change Management"){
ApplicationUser user = userUtil.getUserByKey("sari");
newApprovers.add(user);
}
if (FA == "Corporate Applications")
{
ApplicationUser user = userUtil.getUserByKey("rwillet");
newApprovers.add(user);
}
if (FA == "Config Management") {
ApplicationUser user = userUtil.getUserByKey("whit");
newApprovers.add(user);
}
Approvers.updateValue(null, issue, new ModifiedValue("", newApprovers), changeHolder);
issue.store();

   

Marketa Dvorackova Jul 08, 2015

Thaaaaank you so much, you used the updateValue on the custom field, I just stored the issue and reindexed. This is fantastic. Really thank a lot!!

Janene Hoffmann Jan 13, 2016

We are using the same logic as you to update a "User Picker (multiple users)" custom field (cf). Our cf is named "Managers". Once the field is updated by this script the user names show in the field view of the issue. Problem: If we try to search the field with JQL using "Managers in ("XXXXXXX")" it does not find anything (XXXXXXX = any user id in the field). However, if I actually open the issue, that was updated by the script, select the "Managers" field, don't change anything, and click check-mark to update the field, it will THEN work in JQL to find the issue as stated above. Does anyone else have this problem?

0 votes
Mike Whitlock Dec 17, 2014

Also we are using JIRA 6.1.  Thanks in advance if someone can help.

0 votes
Janene Hoffmann Jan 17, 2016

We are using the same logic as you to update a "User Picker (multiple users)" custom field (cf). Our cf is named "Managers". Once the field is updated by this script the user names show in the field view of the issue. Problem: If we try to search the field with JQL using "Managers in ("XXXXXXX")" it does not find anything (XXXXXXX = any user id in the field). However, if I actually open the issue, that was updated by the script, select the "Managers" field, don't change anything, and click check-mark to update the field, it will THEN work in JQL to find the issue as stated above. Does anyone else have this problem?

Gezim Shehu May 26, 2017 • edited Jun 15, 2017

Hi,

I had a similar issue and the user name got posted in UpperCase. If i just tried to edit the field but do nothing it got updated, but otherwise it didn't work. What fixed it for me, was to re define user names as a new variable like this:

originaluservalue1

def uservalue1 = originaluservalue1.toString().toLowerCase()

This got it working.

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Posted in Off-topic

Disney Live Action Reboots: Are you for or against?

Happy Friday Everyone! Today marks the international release of Disney's live action version of the animated classic Aladdin. I know that this movie was met with some controversy of over cast...

148 views 21 9
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