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

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

This widget could not be displayed.

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();

This widget could not be displayed.

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();

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.

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();

   

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!!

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?

This widget could not be displayed.

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

This widget could not be displayed.

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?

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
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in Teamwork

What teamwork quotes inspire you?

Hey everyone! My name is Natalie and I'm an editor of the Atlassian Blog and I've got a question for you: What's your favorite quote about teamwork?  We've compiled a list here, along with...

196 views 18 7
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you