trying get user from a customfield user picker using groovy script

I am trying to get the user from customfield using groovy but its not working .Please help

Issue issue
UserUtil userUtil = ComponentAccessor.userUtil
def customField = ComponentAccessor.getComponent(CustomFieldManager).getCustomFieldObject("customfield_12202")
def userName = customField.getValueFromIssue(issue)
def user = userUtil.getUserByName(userName).getDirectoryUser()
log.info "check this ${user}\n"

Where am i making the mistake ?

1 answer

This widget could not be displayed.

Assuming customfield_12202 is a user-picker, then your code will have a user object in the variable userName, not just a string, and then you dont need  the "def user" line to do any look up.

I'd suggest

Issue issue
UserUtil userUtil = ComponentAccessor.userUtil
def customField = ComponentAccessor.getComponent(CustomFieldManager).getCustomFieldObject("customfield_12202")
def myUser = customField.getValueFromIssue(issue)
log.info "check this user: " + myUser.getUserName() 

Hi Nic,

I tried that but i am still getting the same error.i put the below code in a workflow postfunction just to test whether i am getting the value or not in the logs.

yes 

customfield_12202 is a user picker .
2017-06-28 15:03:06,837 atlassian-scheduler-quartz1.local_Worker-2 ERROR alenka     [scriptrunner.jira.workflow.ScriptWorkflowFunction] Script 
function failed on issue: JUA-12, actionId: 71, file: <inline script>
java.lang.NullPointerException
        at com.atlassian.jira.issue.customfields.impl.AbstractSingleFieldType.getValueFromIssue(AbstractSingleFieldType.java:79)
        at com.atlassian.jira.issue.fields.CustomFieldImpl.getValue(CustomFieldI
mpl.java:454)
        at com.atlassian.jira.issue.fields.CustomFieldImpl.getValueFromIssue(CustomFieldImpl.java:1467)
        at com.atlassian.jira.issue.fields.renderer.RenderableField$getValueFromIssue$0.call(Unknown Source)
        at Script272.run(Script272.groovy:17)

code i used

Issue issue
UserUtil userUtil = ComponentAccessor.userUtil
def customField = ComponentAccessor.getComponent(CustomFieldManager).getCustomFieldObject("customfield_12202")
def myUser = customField.getValueFromIssue(issue)
log.info "check this user: " + myUser

 

I've just realised something...

You are on JIRA 7.3 or 7.4 aren't you?  My code is for 7.2

no I am trying to get it for 6.4 and also 7.1

 

Abyakta

Try this:

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


def customFieldManager = ComponentAccessor.getCustomFieldManager()
def devOwner = customFieldManager.getCustomFieldObjectByName("UserPicker CF")

String username = issue.getCustomFieldValue(devOwner).name
ApplicationUser user = ComponentAccessor.getUserManager().getUserByName(username);


log.error "Name: " + user.getDisplayName()
log.error "Username: " + user.name

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Published 4 hours ago in Marketplace Apps

The 7 hacks of highly successful automation

...there's anything I've learnt from working, it's that people are lazy! No offense to anyone reading this, but it's true and we can all admit it. The easier you make something for someone, the more...

26 views 0 7
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