Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,457,107
Community Members
 
Community Events
176
Community Groups

Groovy: how to get list of users with spaces

Hello,

There is the multi-user field Specialists. It's necessary to add its list items separated by comma and space, to the Summary via a post-function..

The following script gives the exception:

java.lang.NullPointerException
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

def cField = customFieldManager.getCustomFieldObject("Specialists")
ApplicationUser user = issue.getCustomFieldValue(cField) as ApplicationUser
cField.each {

summary.add(user + " ")

}

  What's wrong with it?

BR

2 answers

1 accepted

1 vote
Answer accepted

Hi @Ivan D 

I've tried the below script on the console and works fine. I've deleted the dedicated issue variable pointing towards a single issue, and I think that it would do the trick:

import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.event.type.EventDispatchOption

def cfmanager = ComponentAccessor.getComponent(CustomFieldManager)
def issueManager = ComponentAccessor.getComponent(IssueManager)
String summary = issue.getSummary()

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def cFieldObj = cfmanager.getCustomFieldObjectByName("Specialists")
def mySize = issue.getCustomFieldValue(cFieldObj).size()

for (int i = 0 ; i < mySize; i++){
   summary = summary + ", " + issue.getCustomFieldValue(cFieldObj)[i]
}

issue.setSummary(summary)
issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)

Hi Alex,

Thanks a lot, it works. However for some reason it puts usernames twice: as username(username in brackets) like user1(user1), user2(user2) etc. Can full names be used instead and once?

Also can the script be modified for using in behaviours?

BR

Hi @Ivan D 

Just place .getName() after the issue.getCustomFieldValue(cFieldObj)[i] :

issue.getCustomFieldValue(cFieldObj)[i].getName()
Why you want to place it on the behavior? I mean, what is the exact business requirement, and how to you want it to work?
Like Ivan D likes this

Hi Alex,

Thank you a lot, it's fixed the issue.

Actually the use case is that - now there is the behaviour that takes a current user to the summary .

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
import static com.atlassian.jira.issue.IssueFieldConstants.SUMMARY

@BaseScript FieldBehaviours fieldBehaviours
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getUser()
def summary = getFieldById("summary")

summary.setFormValue("Education Request ${currentUser.name}")

It's necessary to change it so that instead of the current user it would put Specialists.

Using a post-function is acceptable but it would be better taking values from Specialists during filling in the Create issue form.

BR

@Ivan D 

You can write the following script on the Specialist field behavior side script:

import com.atlassian.jira.component.ComponentAccessor

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def summary = getFieldById("summary")
def specField = getFieldById(getFieldChanged()).getValue()

summary.setFormValue("Education Request ${currentUser.name} ${specField}")

Let me know if that's what you wanted.

Like Ivan D likes this

Hi Alex,

Yes, thank you a lot, that's it. The only thing to improve would be getting a full name instead of username. But changing

getValue()
to
getName()

doesn't work here. Is there any substitution?

BR

Hi @Ivan D 

Sorry for the delayed response but I had little time on my disposal. Here you go:

import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.util.UserManager

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def summary = getFieldById("summary")
def description = getFieldById("description")
def specField = getFieldById(getFieldChanged()).getFormValue() as String
String [] myUsersString = specField.split(", ")
def user
def finalString = ""

for (int i=0 ; i < myUsersString.size() ; i++){
    if (myUsersString.size() > 0){
    user = ComponentAccessor.userManager?.getUserByName(myUsersString[i])
        if (user){
            finalString = finalString + ", " + user.getDisplayName()    
        }
    }
}

if (finalString){
    summary.setFormValue("Education Request " + finalString)
}

I've tested it on my instance and works like the way you want it.

Like Ivan D likes this

@Ivan D kindly let me know if you succeeded with the above script I gave you. It would be great if you could mark my answer as accepted, in order to help others in the community. Thank you!

Hi Alex,

Sorry for the delay, postponed this task.

Thank you a lot for the update, it works with adding full names as intended.

The only drawback is it adds comma after "Education Request , ", that is before the 1st added full name.

If changing it to

finalString = finalString + user.getDisplayName() + ", "

 it instead adds the extra comma after the last full name in the list.

I've marked the answer as accepted because it works in common. My wishes are rather for its improvement.

Thank you!

BR

@Ivan D your requirement, if not mistaken was: 

It's necessary to add its list items separated by comma and space, to the Summary via a post-function.

Then your requirement changed to behaviour script, instead of a workflow post function. I want you to tell me the exact result of what it should appear on the summary. Otherwise we are going to circle around our tail.

Hi Alex,

Sorry for being unclear.

The main goal is: a user shouldn't put anything in the Summary, it's being composed automatically of "Education Request for" and a list of display names from Specialists (multi user picker) separated by a comma and a space. That is like "Education Request for John Johnson, Ann Anderson" where comma and space are only between names.

So there can be 2 approaches by my opinion:

1 (better) - using the Behaviours. Then a user can see what appears in Summary during populating Create screen. Besides editing Summary is possible with adding/removing Specialists if need be.

2 - using a post-function. Then as I see Behaviours should be used anyway to put something in Summary and then a post-function will update it. But it seems in this case editing Summary will reset it by Behaviours value.

Sorry but pros and cons of each approach are being clarified during trials and errors, that's why the initial task can be changed to the more appropriate one.

BR

@Ivan D 

If you want to go by behaviors then try the following. On my instance it's working as expected:

  • The summary is populated with "Education Request for "
  • Until the user has enter at least on user on the Specialist field, the summary will remain as above
  • When he enters one user, then the summary will change to "Education Request for John Doe" without the use of a comma at the end
  • If another user is entered then summary will look like "Education Request for John Doe, Jane Doe" with an automatic use of comma after each user

Try it out and let me know if we nailed it this time.

import com.atlassian.jira.component.ComponentAccessor

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def summary = getFieldById("summary")
summary.setFormValue("Education Request for ")
def description = getFieldById("description")
def specField = getFieldById(getFieldChanged()).getFormValue() as String
String [] myUsersString = specField.split(", ")
def user
def finalString = getFieldById("summary").getFormValue()

for (int i=0 ; i < myUsersString.size() ; i++){
    if (myUsersString.size() > 0){
    user = ComponentAccessor.userManager?.getUserByName(myUsersString[i])
        if (user){
            finalString = finalString + user.getDisplayName()
            if (i < myUsersString.size()-1){
                finalString = finalString + ", "
            }
        }
    }
}

if (finalString){
    summary.setFormValue(finalString)
}
Like Ivan D likes this

Hi Alex,

Thanks a lot, now it works as intended - Summary can be edited any time but only with Specialists list. The only small adding was needed.

def finalString = getFieldById("summary").getFormValue() as String

The question is closed.

BR. 

Nice! Good to know that!

Like Ivan D likes this
0 votes
Hana Kučerová Community Leader Dec 02, 2022

Hi @Ivan D ,

if the field is multi-user, you will get list of users, so you should work with the field like this:

def cField = customFieldManager.getCustomFieldObject("Specialists")
List<ApplicationUser> users = issue.getCustomFieldValue(cField) as List<ApplicationUser>
if (users && users.size() > 0) {
users.each {ApplicationUser user ->
// continue here with getting user name...

}
}

Hi Hana,

Thanks for the feedback. However the script still returns

java.lang.NullPointerException

Also can you please specify the part

// continue here with getting user name...

Will this be working?

summary.add(user + " ")

BR

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events