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

This widget could not be displayed.

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

 

Hi @Hyeonoh Ji

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.

You can use below line.

((LazyLoadedOption)cFieldValu).getValue()

here is the import line

import com.atlassian.jira.issue.customfields.option.LazyLoadedOption;

Thank you @Tuncay Senturk [Snapbytes], 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

def cFieldValue = issue.getCustomFieldValue(cField)
def selectedValue = ((LazyLoadedOption)cFieldValue).getValue()

if (selectedValue.equals("Stream Creation")) {
// do anything here
}

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

@Tuncay Senturk [Snapbytes]

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

Glad to hear that.

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
}

@Tuncay Senturk [Snapbytes]

Sure, will do, thanks alot !!!

You're welcome ;)

This widget could not be displayed.

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;
This widget could not be displayed.

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 I'm New Here May 02, 2018
def cField = customFieldManager.getCustomFieldObject("customfield_10304")

or 

def cField = customFieldManager.getCustomFieldObject(10304L)

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted 3 hours ago in Jira

Atlassian Research Workshop opportunity on Sep. 28th in Austin, TX

We're looking for participants for a workshop at Atlassian! We need Jira admins who have interesting custom workflows, issue views, or boards. Think you have a story to sha...

14 views 0 0
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