Get custom field value of issue using scriptrunner

Mamikon Papikyan May 26, 2020

Hi, I added new custom field in my project. And now I want to get my custom field value, when I'm clicking the button of "Create" issue, so and I'm using this code

import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.component.ComponentAccessor;

//Debug
def log = Logger.getLogger("com.acme.CreateSubtask")
log.setLevel(Level.DEBUG)


def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject("10301")
def cFieldValue = issue.getCustomFieldValue(cField)

log.info("Field Value="+cFieldValue+"URL="+url+" Status="+connection.getResponseCode() as
String)

 But I'm getting this error... this is my log:

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2020-05-26 13:40:41,732 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2020-05-26 13:40:41,733 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: null
java.lang.NullPointerException
	at com.atlassian.jira.issue.IssueImpl.getCustomFieldValue(IssueImpl.java:896)
	at com.atlassian.jira.issue.Issue$getCustomFieldValue$2.call(Unknown Source)
	at Script123.run(Script123.groovy:23)

 What's wrong?

1 answer

1 accepted

1 vote
Answer accepted
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 26, 2020

See https://docs.atlassian.com/software/jira/docs/api/8.5.1/com/atlassian/jira/issue/CustomFieldManager.html#getCustomFieldObject-java.lang.Long-

Try either 

def cField = customFieldManager.getCustomFieldObject(10301)

Or 

def cField = customFieldManager.getCustomFieldObject("customfield_10301")
Mamikon Papikyan May 27, 2020

Thank you!! The second variant very helpful...("customfield_10301"). It works perfectly..

Like # people like this

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
PERMISSIONS LEVEL
Site Admin
TAGS
AUG Leaders

Atlassian Community Events