Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Scriptrunner - Get value of a custom field User Picker (single) - Value NULL

Tom December 20, 2022

Hello 
I like to get the value of custom field User Picker (single). The value cfVal allways is null.
This example I was searching and found many times, but can't solve the problem.

 

import com.atlassian.jira.component.ComponentAccessor;


def cf = customFieldManager.getCustomFieldObject("customfield_11501")
def cfVal = issue.getCustomFieldValue(cf)

def check = "cf: " + cf + " / cfVal: " + cfVal
issue.setDescription(check)

3 answers

0 votes
Tom December 22, 2022

Problem solved!

By cloning an Isssue Issue.getCustomFieldValue() points to target issue

In my case I need sourceIssue.getCustomFieldValue() to get the source issue.
Thanks to Mohamed and Ram for your help!
0 votes
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
December 22, 2022

Hi @Tom

I have run a basic test in my environment and don't seem to be encountering any issues.

Below is the sample code that I have tested with:

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.customFieldManager
def sampleUserPicker = customFieldManager.getCustomFieldObjectsByName('Sample User Picker').first()
def sampleUserPickerValue = issue.getCustomFieldValue(sampleUserPicker).toString()
def check = "cf: ${sampleUserPicker} / cfVal: ${sampleUserPickerValue}"

issue.setDescription(check)

Please note that the sample code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

Below is a screenshot of the Post-Function configuration:-

post_function_config.png

Below are a couple of test screenshots:-

1. First, the Description field is set to blank when the Issue is created. Also, you will notice that a Custom Field called Sample User Picker is filled with the Admin user.

test1.png

2. Once the Issue transitions to the In Progress status, the Description field is updated with the values set in the code.

test2.png

 

Regarding why you are getting the Null value, I suspect the value for the User Picker field has not been set yet. (I could be wrong)

I hope this helps to solve your question. :-)

Thank you and Kind regards,

Ram

0 votes
Mohamed Benziane
Community Champion
December 20, 2022

Hi,

Welcome to the community

I think this script should do the work

 

import com.atlassian.jira.component.ComponentAccessor;

def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("yourIssueKey")
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObject("customfield_11501")
def cfVal = issue.getCustomFieldValue(cf)

log.warn(cfVal.displayName)
Tom December 20, 2022

Hi Mohamed
Thanks for your answer. I think the problem is "yourIssueKey".
My script is running as post function to clone the actual issue.

Mohamed Benziane
Community Champion
December 20, 2022

I see, so you can use 'issue' like you first script to get the current issue.

 

import com.atlassian.jira.component.ComponentAccessor;

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObject("customfield_11501")
def cfVal = issue.getCustomFieldValue(cf)

log.warn(cfVal.displayName)
Tom December 21, 2022

In this case cfVal is NULL. I tried to add

import com.atlassian.jira.issue.Issue;

def issueKey = issue.getKey()
but then no clone of my story is generated

Suggest an answer

Log in or Sign up to answer