Groovy script to append current user to multi-user picker

Joshua DeClerck October 19, 2016

I have an approval-style workflow post-function that appends the current user to a multi-user picker custom field. So far I've used the non-groovy "Update Issue Custom Field" post-function to append "%%CURRENT_USER%%", which doesn't behave reliably for all types of usernames. Since that method has no flexibility, I'd like to switch over to a groovy script.

The behavior ultimately relies on comparing two multi-user picker fields, "Required Approvers" and "Approved by". An "Approve" transition has been set up expecting two conditions and two post-functions:

  1. Condition - the current user is listed in "Required Approvers".
  2. Condition - the current user is not listed in "Approved by".
  3. Post-function - append the current user to "Approved by".
  4. Post-function - ScriptRunner fast-track transition to the next phase of the workflow if the following condition is met:

    cfValues['Approved by'] as Set == cfValues['Required Approvers'] as Set

The comparison in (4) breaks down because certain usernames (mixed case) aren't stored by "Update Issue Custom Field" (3) the same way they're stored when manually entered into the "Required Approvers" field.

In trying to re-do (3) with ScriptRunner, I'm struggling to find any up-to-date examples of appending currentUser to this type of field. The closest I've found has ultimately boiled down to using an operation like this:

Approvers.updateValue(null, issue, new ModifiedValue("", user), changeHolder);

(based on code found here)

However, the ModifiedValue() part doesn't seem to be valid code anymore in the latest versions of JIRA+SR. Or at least, every example of it that I've been able to find throughout the community now returns errors.

Is there a newer, more recommended way to do this?

Thanks in advance for any help!

2 answers

1 accepted

3 votes
Answer accepted
Vasiliy Zverev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 19, 2016

Here is code example to do this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.ApplicationUser

CustomField multiUser = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("fieldName")
List<ApplicationUser> users = issue.getCustomFieldValue(multiUser)
users.add(ComponentAccessor.getUserManager().getUserByName("username"))

issue.setCustomFieldValue(multiUser, users);
Joshua DeClerck October 20, 2016

Thanks @Vasiliy Zverev! Starting from this, I was able to end up with something that seems to be working the way I hoped. I ran into a type checking error on line 7, but appending "as List" cleared it. The only other problem was that add() fails when users is null, but that was a simple fix, too. All in all, very happy -- thank you!

Bridgestone February 25, 2017

This code doesn't work if the multiuser picker field "fieldname" doesn't contain anything. I'm getting this error: 

ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: null, actionId: 1, file: <inline script>
java.lang.NullPointerException: Cannot invoke method add() on null object

 

Any idea? Thanks!

Vasiliy Zverev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 26, 2017

Lets try this one

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.ApplicationUser

CustomField multiUser = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("fieldName")
List&lt;ApplicationUser&gt; users = issue.getCustomFieldValue(multiUser)
if(users == null)
    users = new ArrayList&lt;&gt;();

users.add(ComponentAccessor.getUserManager().getUserByName("username"))

issue.setCustomFieldValue(multiUser, users);
Manjunatha K R July 3, 2017

Hi Vasiliy,

Thanks for sharing this scripts, works well for us to copy a Jira issue Reporter name into multi user customfield value. In multi user, we are not appending the value as it's having one name only as of now.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.ApplicationUser

def componentCF2 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("ITG To Test")
String selectedComponents2 = issue.getCustomFieldValue(componentCF2)

def componentCF1 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Defect Source")
String selectedComponents1 = issue.getCustomFieldValue(componentCF1)

if(selectedComponents1 == "Design Server" && selectedComponents2 == "NO")
{
String reporter = issue.reporter?.name
issue.setAssigneeId(reporter)

String reporter1 = issue.reporter?.name
CustomField multiUser = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Test Managers")
List<ApplicationUser> users;
//= (List<ApplicationUser>) issue.getCustomFieldValue(multiUser)
if(users == null)
    users = new ArrayList<>();

users.add(ComponentAccessor.getUserManager().getUserByName(reporter1))

issue.setCustomFieldValue(multiUser, users);
}    
Manjunatha K R July 3, 2017

Same postfunction working script doen's work in when we use in Fast-track transition - not sure why?

ScriptRunner workflow function - Fast-track transition an issue (condition & additional actions apply

- Manju

Manjunatha K R July 3, 2017

Thanks for sharing the above script Vasiliy Zverev,

I customized your script to copy/overwrite Reporter of the Jira issue to other multi user picker custom field i,e Test Managers value in my post function, it working as expected for our usage.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.ApplicationUser

def componentCF2 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("ITG To Test")
String selectedComponents2 = issue.getCustomFieldValue(componentCF2)

def componentCF1 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Defect Source")
String selectedComponents1 = issue.getCustomFieldValue(componentCF1)

if(selectedComponents1 == "Design Server" && selectedComponents2 == "NO")
{
String reporter = issue.reporter?.name
issue.setAssigneeId(reporter)

String reporter1 = issue.reporter?.name
CustomField multiUser = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Test Managers")
List<ApplicationUser> users;
//= (List<ApplicationUser>) issue.getCustomFieldValue(multiUser)
if(users == null)
    users = new ArrayList<>();

users.add(ComponentAccessor.getUserManager().getUserByName(reporter1))

issue.setCustomFieldValue(multiUser, users);
}    
0 votes
Alibek Malikov February 28, 2019
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.ApplicationUser

CustomField multiUser = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("fieldName")
List&lt;ApplicationUser&gt; users = issue.getCustomFieldValue(multiUser)
if(users == null)
    users = new ArrayList&lt;&gt;();

users.add(ComponentAccessor.getUserManager().getUserByName("username"))

issue.setCustomFieldValue(multiUser, users);


!!!I use this script in the post-function but it gives an error!!!

The script could not be compiled: <pre>org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script23.groovy: 9: unexpected token: ; @ line 9, column 28. users = new ArrayListlt;&gt;(); ^ 1 error </pre>.

 

 What have I done wrong?

Vasiliy Zverev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 3, 2019
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.ApplicationUser

CustomField multiUser = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("fieldName")
List&lt;ApplicationUser&gt; users = issue.getCustomFieldValue(multiUser)
if(users == null)
    users = new ArrayList();

users.add(ComponentAccessor.getUserManager().getUserByName("username"))

issue.setCustomFieldValue(multiUser, users);

Try this one

Like Alibek likes this
Joshua DeClerck March 4, 2019

Just in case... check that all instances of &lt; and &gt; are changed into less-than and greater-than signs, respectively. I guess we aren't allowed to type those here.

Like Alibek likes this

Suggest an answer

Log in or Sign up to answer