How to copy watchers to custom field (user picker - multiple) with Script Runnerc

Hi - I need to set a custom field for our clients users, who are not jira-software-users. We're using third party helpdesk plugin to work with them, and I need a custom scripted field to filter watchers. Client's users have "@" character in their usernames, so it's fairly easy. The problem is with User Picker templates - I can't get it to show any results.

 

I tried both single and multiple user template. Single always shows "anonymous" in script preview, and multiple basically shows nothing. Please advise. This is my script:

 

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.watchers.IssueWatcherAccessor
import com.atlassian.jira.issue.watchers.WatcherManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.ApplicationUsers
IssueWatcherAccessor iwa = ComponentAccessor.getComponent(IssueWatcherAccessor.class)
def list =  iwa.getWatcherKeys(issue)
List filtered = new ArrayList();
for (user in list){
    // IF/ELSE does the same for test purpose !!!
	if(user==~ /.*@.*/){
    	filtered.add(ApplicationUsers.toDirectoryUser(ApplicationUsers.byKey(user)))
    }else{
        filtered.add(ApplicationUsers.toDirectoryUser(ApplicationUsers.byKey(user)))
    }
}
List<ApplicationUser> clientWatchers = ApplicationUsers.from(filtered)
log.error clientWatchers
return clientWatchers

 

I'm loggin "clientWatchers" to see what it returns:

log.png

 

 

user picker (single user) shows Anonymous in script preview. 

 

Zrzut ekranu 2016-06-02 o 18.23.29.png

 

BUT it does exactly the same whatever I return, example: 

 

Zrzut ekranu 2016-06-02 o 18.27.52.png

 

and multiple user picker shows nothing:

 

Zrzut ekranu 2016-06-02 o 18.24.27.png

 

"test" custom field remains empty in issue view:

Zrzut ekranu 2016-06-02 o 18.21.48.png

 

Either I'm doing something wrong, or user picker templates are broken. Please advise.

1 answer

1 accepted

1 vote

Does the "log" line show the correct things?

I see this problem when passing the wrong type of User, but you seem to be using ApplicationUser, which is correct - depending on the version of JIRA. What version are you using?

I am not sure why you are converting from appuser to directory user and back again, why don't you just let "filtered" be a list of AppUsers?

In fact why don't you just filter on com.atlassian.jira.issue.watchers.IssueWatcherAccessor#getWatchers, rather than their keys?

Actually - I suspect the problem is you have the free text searcher on. Make sure you are using the Multi User Picker Searcher, otherwise it coerces the return value to be suitable for the indexer.

 

image2016-6-2 17:46:18.png

Note - in the screenshot it shows free text searcher, but actually I changed it to multi user search in another window, then it worked properly.

That was it - searcher smile Thank you so much @Jamie Echlin [Adaptavist] for your prompt response and help!

No problem. And thanks for your review Tomasz, greatly appreciated wink

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
Posted Wednesday in Jira

Join our webinar: How 1B+ feature flag events helped us build the new Jira

Every time you release software, there's a bit of risk – that there's a bug, that something breaks, or that the feature doesn't resonate with customers. Feature flagging helps make high stakes s...

134 views 0 3
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