It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

Deleted user Mar 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

11 votes
Tuncay Senturk Community Leader Mar 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 Mar 15, 2016

Last line should probably be:

issue.setSummary(cFieldValue + " " + issue.summary)
Tuncay Senturk Community Leader Mar 15, 2016

My bad, copy paste smile 

I changed the code accordingly

Deleted user Mar 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 Mar 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..

Deleted user Mar 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 Jul 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] Community Leader Apr 06, 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 Apr 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 Apr 25, 2018

You can use below line.

((LazyLoadedOption)cFieldValu).getValue()

here is the import line

import com.atlassian.jira.issue.customfields.option.LazyLoadedOption;
Naveen Apr 26, 2018 • edited

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 Apr 26, 2018
def cFieldValue = issue.getCustomFieldValue(cField)
def selectedValue = ((LazyLoadedOption)cFieldValue).getValue()

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

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

Naveen Apr 26, 2018

@Tuncay Senturk

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

Tuncay Senturk Community Leader Apr 26, 2018

Glad to hear that.

Tuncay Senturk Community Leader Apr 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 Apr 26, 2018

@Tuncay Senturk

Sure, will do, thanks alot !!!

Tuncay Senturk Community Leader Apr 26, 2018

You're welcome ;)

0 votes
ANN LEE Jul 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;
0 votes
Lotus Support Jun 13, 2017 • edited

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 Evans May 02, 2018
def cField = customFieldManager.getCustomFieldObject("customfield_10304")

or 

def cField = customFieldManager.getCustomFieldObject(10304L)
Like Akshay Sawan likes this

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Next-gen

Introducing subtasks for breaking down work in next-gen projects

Teams break work down in order to help simplify complex tasks. This is often done iteratively, with tasks being broken down into smaller tasks and so on until the work is accurately captured in well-...

10,817 views 49 52
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you