Scripted Field always returning Anonymous User

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?

2 answers

1 accepted

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).

0 vote
Tayyab Bashir Community Champion Oct 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. 

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.

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 yesterday in Jira

Mission-critical battery manufacturer fulfills FAA software requirements with Commit Policy Plugin

EaglePicher Technologies is a leading manufacturer of battery systems for diverse industries like defense, aviation, space or medical. As they operate in highly regulated industries, keeping a clear ...

117 views 0 2
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