Scripted field to list full names from multi user custom field

I am trying to create a scripted field which will populate with a list of users full names from a multi user picker field ("Award Nominees" customfied_21775)

I have tried the following script:

 

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.Issue

import com.atlassian.jira.issue.fields.CustomField

import com.atlassian.jira.user.ApplicationUser

 

CustomField multiuserCstFld = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Award Nominees")

if (multiuserCstFld == null)

    return "custom field not found"

 

StringBuilder result = new StringBuilder();

 

for(ApplicationUser user: (ArrayList) multiuserCstFld.getValue(issue))

    result.append(user.getName() + "/")

 

return result.toString().substring(0, result.toString().length() - 1)

 

but I get the following syntax error:

[Static type checking] - Cannot loop with element of type com.atlassian.jira.user.ApplicationUser with collection of type java.util.ArrayList

 

I'm at a loss as to what is wrong.  I am on JIRA version 7.1.9 and using scriptrunner version 4.3.13

 

Any help would be greatly appreciated.  Thank you!

2 answers

1 accepted

If you want their full names, you'll want to use the getDisplayName method. 

import com.atlassian.jira.component.ComponentAccessor
def awardNomineesField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Award Nominees")
if (!awardNomineesField) { return null }
issue.getCustomFieldValue(awardNomineesField)?.collect{ 
    it.displayName 
}?.join(" / ")

You are correct.  I did need to change getname to getDisplayName to show the full name.  Thank you.

1 vote
Vasiliy Zverev Community Champion Dec 22, 2016

Try this code

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

CustomField multiuserCstFld = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Award Nominees")

if (multiuserCstFld == null)
    return "custom field not found"

StringBuilder result = new StringBuilder();

for(ApplicationUser user: (ArrayList<ApplicationUser>) multiuserCstFld.getValue(issue))
    result.append(user.getName() + "/")

return result.toString().substring(0, result.toString().length() - 1)

That was it.  I was missing the <ApplicationUser> after the ArrayList. No more error! Thank you very much!

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Sunday in Agility

You asked for it, so we delivered: images on issues have arrived

A picture tells a thousand words. And agility boards have just released their latest feature: cover images on issues – so now your board can tell a story at first glance. Upload attachmen...

231 views 2 11
Read article

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