How to get the custom field value using by groovy script?

Deleted user March 15, 2016

Hello 

I want to get a value of customfield using by groovy script..

And then, I want to set a prefix on issue summary using by customfield value..

it is an example below;

 

  • custom field name = 'Test Field' (single select list field type)
  • custom field name value = 'apple'

 

Then first of all... I want to get the value what is 'apple' by groovy script.

and next i want to set a summary..

 

Actually I know setting a summary by groovy like below

    import com.atlassian.jira.issue.Issue

    issue.setSummary("Test Summary");

 

Then it will be set a "Test Summary" as a new issue summary..

However what I want to do is "issue.setSummary(customfieldvalue + summay)"

 

So...How can I get a customfield value (single select list field type) and how can set a summary..?

Additionally.. I will set it by post-function..

 

Please let me know.. thanks..

 

 

 

 

 

 

 

3 answers

1 accepted

17 votes
Answer accepted
Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 15, 2016

Can this help?

def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject("customfield_id")
def cFieldValue = issue.getCustomFieldValue(cField)
 
 issue.setSummary(cFieldValue + " " + issue.summary);
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.
March 15, 2016

Last line should probably be:

issue.setSummary(cFieldValue + " " + issue.summary)
Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 15, 2016

My bad, copy paste smile 

I changed the code accordingly

Deleted user March 16, 2016

I tried below;

 

 

import com.atlassian.jira.component.ComponentAccessor;
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;
def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject("10304")
def cFieldValue = issue.getCustomFieldValue(cField)

issue.setSummary(cFieldValue + " " + issue.summary);

 

However it was not working..

I think they have some problem but I don't know why..

def cField = customFieldManager.getCustomFieldObject("10304")
def cFieldValue = issue.getCustomFieldValue(cField)
Deleted user March 16, 2016

ahhh.....

it was it..

def cField = customFieldManager.getCustomFieldObject("customfield_10304")

 

However what I told about customfield type was "Selecet List" type..

It works from "Text Field" but I think it doesn't work from "Select List" Type..

Like Liudvikas likes this
Deleted user March 16, 2016

I solve it..

Thanks to Tuncay Senturk and Jamie Echlin.. smile

Also if you don't mind, can you recommend some of sites to learn groovy script?

ANN LEE July 21, 2016

By following the previous example to get customer field (single line text) value. But still fails! 

Could you please let me know? Many thanks!

 

def customFieldManager = ComponentAccessor.getCustomFieldManager()

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

def lastnameTypeCF = customFieldManager.getCustomFieldObject("Last Name")
def lastnameFieldV = issue.getCustomFieldValue(lastnameTypeCF)

issue.setSummary(summary + " - " + firstnameFieldV + " " + lastnameFieldV)

 

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

2016-07-21 10:39:21,458 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2016-07-21 10:39:21,458 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: TPSHR-1038, 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$3.call(Unknown Source)
	at Script104.run(Script104.groovy:72)
Thanos Batagiannis _Adaptavist_
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.
April 6, 2017

Hi Ann, 

The 

ComponentAccessor.getCustomFieldManager().getCustomFieldObject("First Name") 

get as a param the String representation of the custom field's id, should be something like, customfield_12345

So either you have to pass as a param the id or if you want to use the name try

// be aware if you have more than one custom fields with the same name
ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Name of the custom field")

 

Naveen April 25, 2018

Hi @[deleted]

How did you solve the Single Select List  Custom field issue that you were having above?

As you mentioned i could able to extract out Text Field Value but not Single Select List field value. How did you got the value for that field, could you please post here.

Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 25, 2018

You can use below line.

((LazyLoadedOption)cFieldValu).getValue()

here is the import line

import com.atlassian.jira.issue.customfields.option.LazyLoadedOption;
Like Szpecku likes this
Naveen April 26, 2018

Thank you @Tuncay Senturk, i am very bad at coding, i am using it like below, but it is throwing an error, please correct me

 

import com.atlassian.jira.component.ComponentAccessor;
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.customfields.option.LazyLoadedOption;
def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject("customfield_12512")
def cFieldValue = ((LazyLoadedOption)cField).getValue()

if (cFieldValue == "Stream Creation")
{
issue.summary = "Stream request has been Submitted"
}

 

Regards,

Naveen

Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 26, 2018
def cFieldValue = issue.getCustomFieldValue(cField)
def selectedValue = ((LazyLoadedOption)cFieldValue).getValue()

if (selectedValue.equals("Stream Creation")) {
// do anything here
}
Naveen April 26, 2018

Ok thank you again :), i will try this and let you know how it goes.

Naveen April 26, 2018

@Tuncay Senturk

That did the trick, thanks a lot for all the help, greatly appreciated !!!

Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 26, 2018

Glad to hear that.

Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 26, 2018

By the way, do not forget to check nulls.

 

def cFieldValue = issue.getCustomFieldValue(cField)
if (null == cFieldValue) return;
def selectedValue = ((LazyLoadedOption)cFieldValue).getValue()
// and also hard coded string should be put before the equality, as below
if ("Stream Creation".equals(selectedValue)) {
// do anything here
}
Naveen April 26, 2018

@Tuncay Senturk

Sure, will do, thanks alot !!!

Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 26, 2018

You're welcome ;)

0 votes
Lotus Support June 13, 2017

Using code i am getting error

import com.atlassian.jira.component.ComponentAccessor;
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;
def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject("10304")
def cFieldValue = issue.getCustomFieldValue(cField)

issue.setSummary(cFieldValue + " " + issue.summary);

error.jpg

huw May 2, 2018
def cField = customFieldManager.getCustomFieldObject("customfield_10304")
Alejandro Suárez - TecnoFor
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
June 13, 2018

or 

def cField = customFieldManager.getCustomFieldObject(10304L)
Like # people like this
0 votes
ANN LEE July 21, 2016

Also, I imported all required libraries at the very beginning of this script:

import com.atlassian.jira.component.ComponentAccessor;
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;

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events