How can i get Custom field values of workflow using groovy?

AKASHB July 8, 2013
How can i get the custom field value in jira using groovy on my terminal under log file.

3 answers

1 accepted

0 votes
Answer accepted
Tsol
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.
July 8, 2013

If i got it right, you want to get the value of a custom field in jira logs, when a workflow is executed.

If so, use a groovyscript in your post function like the following:

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue; 


CustomFieldManager customFieldManager = ComponentManager.getInstance().getCustomFieldManager();
 
CustomField customField_name = customFieldManager.getCustomFieldObjectByName( "customfield_name" );


log.warn ("value: "+ issue.getCustomFieldValue( customField_name))

Test it first.

Cheers

AKASHB July 9, 2013
Thanks kostas, Thats what i exactly wanted. can i perform transition associated with this custom fieldd?
Tsol
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.
July 9, 2013

What do you mean by "can i perform transition associated with this custom field?"

You just have to create a file and use it in a scripted post function of your workflow.

Have a look at jamie's plugin page and you will clarify most of things.

ANN LEE September 9, 2016

I tried to use above code, but seems 

ComponentManager.getInstance().getCustomFieldManager(); decrecated. 

How can I get single line customer value using JIRA 7.1.2 API?

Thanks!

ANN LEE September 9, 2016

I tried to use above code, but seems 

ComponentManager.getInstance().getCustomFieldManager(); decrecated. 

How can I get single line customer value using JIRA 7.1.2 API?

Thanks!

JamieA
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.
September 12, 2016

Use:

import com.atlassian.jira.component.ComponentAccessor

ComponentAccessor.getCustomFieldManager()
ANN LEE September 12, 2016

I did have following lines in my post:

 

import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def firstnameTypeCF = customFieldManager.getCustomFieldObject("First Name")
def firstnameFieldV = parentIssue.getCustomFieldValue(firstnameTypeCF)

But failed with following error:

2016-09-12 13:06:51,034 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2016-09-12 13:06:51,041 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: TPSHR-1372, actionId: 1, file: <inline script>
java.lang.NullPointerException
	at com.atlassian.jira.issue.IssueImpl.getCustomFieldValue(IssueImpl.java:896)
	at com.atlassian.jira.issue.Issue$getCustomFieldValue$1.call(Unknown Source)
	at Script58.run(Script58.groovy:71)

 

I don't know why it's failed?

Thanks!

 

 

Jonny Carter
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 12, 2016

The CustomFieldManager.getCustomFieldObject method signature takes a custom field ID (either a Long or a string that has the numeric ID). You want the ...ByName signature that Kostas used. Otherwise, the custom field manager will probably return null, and you'll get that null pointer exception.

import com.atlassian.jira.component.ComponentAccessor;

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def firstName = customFieldManager.getCustomFieldObjectByName("First Name");
def firstnameFieldV = parentIssue.getCustomFieldValue(firstName)

I'm also assuming that the parentIssue variable is defined elsewhere, and that you've omitted it for brevity. If not, you may need to manually define that, depending on the context you're running this script under.

ANN LEE October 12, 2016

I tried your suggested code and it works well!

Thank you so much for your great help!

 

Ann

0 votes
AKASHB July 9, 2013
Thanks kostas, Thats what i exactly wanted. can i perform transition associated with this custom fieldd?
0 votes
JamieA
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.
July 8, 2013

I have no idea what you mean. If Kostas' answer does not help, can you explain a bit more what you're trying to do.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events