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

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

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);

Last line should probably be:

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

My bad, copy paste smile 

I changed the code accordingly

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)

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..

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?

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)

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")

 

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;

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

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

2,811 views 11 18
Join discussion

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot