Scripted Field always returning Anonymous User

PhillipS October 24, 2017

Yesterday I wrote a custom script that was returning a user based off a userName and it was working.  This morning I ran the same code and it was returning Anonymous User.  I simplified the code just to return me a static user and return it, and it still returns the Anonymous User.

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.user.ApplicationUser

ApplicationUser u = ComponentAccessor.getUserManager().getUserByKey("XXXX")
return u

 Some things I have verified with the above code snippet:

* XXXX is my username in jira (what I use to login with - omitted for security)

* The Template type is "User Picker (single user)"

 

The interesting thing is that when I change the template type to "Text Field (multi-line)", and do the following:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.user.ApplicationUser

ApplicationUser u = ComponentAccessor.getUserManager().getUserByKey("XXXX")
return u.getDisplayName()

 I get what I expect. My full name

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.user.ApplicationUser

ApplicationUser u = ComponentAccessor.getUserManager().getUserByKey("XXXX")
return u.getEmailAddress()

 I get what I expect. I get my email address

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.user.ApplicationUser

ApplicationUser u = ComponentAccessor.getUserManager().getUserByKey("XXXX")
return u.getUserName()

 I get what I expect.  My original input "XXXX"

 

Either something really bad is happening here, or I'm missing something really simple.  Can someone please help out?

3 answers

1 accepted

0 votes
Answer accepted
PhillipS October 24, 2017

Very strange...I created a new scripted field, copied the first bit of code and it started working again.  My only guess is that there is a bug when constantly switching the return type between Text Field (multi-line) and User Picker (which i've been doing for debugging).

5 votes
Tayyab Bashir
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 25, 2017

Hi,

The reason for it returning Anonymous user is that you probably are incorrectly mismatching the Searcher and the Template. 
If you have the template as "Users Picker" and the Searcher is set as "Free Text Searcher", then you would get Anonymous in your Field. 
Because there's a mismatch between searcher and the template. 

So if you are using User Picker template, set the Searcher for User picker as well. 
And likewise for Free text template and searcher. 

PhillipS October 27, 2017

Although this is the most likely thing that was wrong (and I found so in other community questions), I played around with it for multiple hours switching it between user picker, and free text searcher.  and it still returned Anonymous user when using "User Picker".  I also verified that the type returned was strongly typed as an ApplicationUser.  as I mentioned above, I think there was something corrupt in the database with this 1 scripted field, because once I spun up a new one, copy/pasted the embedded code, it started working again as expected.

1 vote
Esteban Diaz January 17, 2019

Hi Phillips, I´m having the same problem here....  did you find any solution?

Josh_Kochelek__Modus_Create
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.
August 28, 2019

Hey @Esteban Diaz. I had a similar issues that was resolved by updating the Custom Field Search Template. I posted the answer to my own question here: 

Scripted Field - How to return multiple users

Suggest an answer

Log in or Sign up to answer