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

Mike Whitlock December 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 December 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
Janene Hoffmann January 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 [Communardo]
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 26, 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.

0 votes
Mike Whitlock December 17, 2014

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

0 votes
Mike Whitlock December 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 July 7, 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 July 8, 2015

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 July 8, 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 January 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?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events