setting customfield value is not working

Here is the code I am using to assign value for custom field (select list(single choice)).

import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def textCf = customFieldManager.getCustomFieldObjectByName("customFieldId=10807")
issue.setCustomFieldValue(textCf, "Training")

when I used it in custom post function, it did not give me any error. But it is not working also. please give me solution how can I assign value to my custom field.

Script runner version : 4.1.3

JIRA version: 6.4.9

3 answers

1 accepted

1 vote

You say that your field is a select list (single), but then you treat it as though it is a text field.  A select list expects you to give it an option not a string.

 
The code snippet I have replaces the last couple of lines in your code:
def cf = customFieldManager.getCustomFieldObjectByName("customFieldId=10807")
def cfConfig = cf.getRelevantConfig(issue)
value = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find { it.toString() == 'Training' }
issue.setCustomFieldValue(cf, value)

Even using code above, it is not assign value to my custom field. And it is not giving any error in script.

What does the log file say when it runs? Where are you running and testing it? Have you tried adding any debug lines to it?

Log file says : "cannot invoke method getRelevantConfig() on Null object". I am running this script on Test project's workflow transition custom script. I used above code only. but still it gives error in log file.

That probably means that the field does not exist for the project/issue-type you are trying to work with.

this is wrong: getCustomFieldObjectByName("customFieldId=10807") it should be: getCustomFieldObjectByName("Name of custom field")

Also, if it was looking for a custom field id it would be "customFieldId_10807".

actually customfield_10807 :-)

Gah, sorry.

It was also missing the "def value".

For clarity, here is what the proposed post function script looks like:

import com.atlassian.jira.component.ComponentAccessor
def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Name of custom field")
def cfConfig = selectCf.getRelevantConfig(issue)
def value = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find {
    it.toString() == 'Training'
}
issue.setCustomFieldValue(cf, value)

https://www.adaptavist.com/doco/display/SFJ/Cannot+set+a+custom+field+value+for+a+select+list

Thank you. Its working now.
now I want to add if.. else condition to this code.
If (customfield_value1 == 'Project1')
{
issue.setCustomFieldValue(cf, value1)
}
else if (Customfieldvalue1 == 'Project2')
{
issue.setCustomFieldValue(cf, value2)
}
However getting customfield value and comparing it is challenging. Can you tell me with example above how to write code for this?

depends on the type of customfield that you are comparing with Project1 etc, what type is it?

type of both customfielda are select list(single choice).

so say cfProject is the project cf instance, it would be: if (issue.getCustomFieldValue(cfProject)?.value == "Project"1) {...}

Hi Jamie, I used script below to check what is the vale of "Target" customfield of type (select list(single choice)) and assign value of customfield "Serv.Req Acceptance and Handover Activity" of type (select list(single choice)) using nested if .. else condition. I used this condition on workflow transition custom scipt post function. However it is not giving any error, but not assign value as per condition, in fact below code deoes not work properly. It neither give any error, nor assign value to custom field. import com.atlassian.jira.component.ComponentAccessor def customFieldManager = ComponentAccessor.getCustomFieldManager() def cf = customFieldManager.getCustomFieldObjectByName("Serv.Req Acceptance and Handover Activity") def cfConfig = cf.getRelevantConfig(issue) def value = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find { it.toString() == 'Off gate change - accepted' } def value1 = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find { it.toString() == 'Off gate solution - accepted' } def value2 = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find { it.toString() == 'Review changes' } def cfProject=customFieldManager.getCustomFieldObjectByName("Target") if (issue.getCustomFieldValue(cfProject) == "Request for change") { issue.setCustomFieldValue(cf, value) } else if(issue.getCustomFieldValue(cfProject) == "Request for solution") { issue.setCustomFieldValue(cf, value1) } else { issue.setCustomFieldValue(cf, value2) }

Check your data types - for example issue.getCustomFieldValue(cfProject) == "Request for change" If I've read your code correctly, the custom field is a select list, so the value is an option, not a string.

even using correct script like option foe select list, it is not working. import com.atlassian.jira.component.ComponentAccessor def customFieldManager = ComponentAccessor.getCustomFieldManager() def cf = customFieldManager.getCustomFieldObjectByName("Serv.Req Acceptance and Handover Activity") def cfConfig = cf.getRelevantConfig(issue) def value = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find { it.toString() == 'Off gate change - accepted' } def value1 = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find { it.toString() == 'Off gate solution - accepted' } def value2 = ComponentAccessor.optionsManager.getOptions(cfConfig)?.find { it.toString() == 'Review changes' } def cfProject=customFieldManager.getCustomFieldObjectByName("Target") def cfConfig1 = cfProject.getRelevantConfig(issue) def condition=ComponentAccessor.optionsManager.getOptions(cfConfig1)?.find { it.toString() == 'Request for change' } def condition1=ComponentAccessor.optionsManager.getOptions(cfConfig1)?.find { it.toString() == 'Request for solution' } if (condition == "Request for change") { issue.setCustomFieldValue(cf, value) } else if(condition1 == "Request for solution") { issue.setCustomFieldValue(cf, value1) } else { issue.setCustomFieldValue(cf, value2) } it is not updating value as expected.

At a glance 1. I don't think your braces are doing what you want? Check the if {} then {} else {} structures are correct 2. Your if statements are still comparing options with strings.

If()... else().. condition syntax is correct. But it is not able to set custom field values according to if_else condition . can you elaborate how to set custom field value (of type select list single choice) using one example. As the code above is not giving any error, I am not able to resolve it. Waiting for your help.

Your code for setting the values is ok. The fact it's not throwing errors implies that it's simply not reaching the code that sets it. Hence the feeling that it's your if blocks and comparisons that are wrong.

Thanks for the answer :)

Hi Tejashree,

     You resolved this Issue? We are facing same problem. We have written one dropdown list name['EMployee']. In Employee Field there are 4 employee name ex{john, mark, sham, george}. Suppose we select 'mark' in dropdownlist. Then how should i get value of this field using groovy script?. We need 'mark' string value. and we need to set this value in another field.
         Can you please help us. we are using 7.3.0 jira version. Thanks in advance.

It's this one: https://scriptrunner.adaptavist.com/latest/jira/recipes/workflow/validators/simple-scripted-validators.html#_validating_a_multi_select_option_is_selected

So to get the value you would use:

cfValues['Employee']?.value

issue.getCustomFieldValue(...).value, which will be string is this is a single select list.

cfValues['Employee']?.value is not useful in custom script post function.

I have resolved this issue. here is my code

 

import com.atlassian.jira.component.ComponentAccessor;

import com.atlassian.jira.issue.CustomFieldManager;

def customFieldManager =

ComponentAccessor.getCustomFieldManager()

def firstnameTypeCF = customFieldManager.getCustomFieldObjectByName("Emp")

def outputField = customFieldManager.getCustomFieldObjectByName("Output")

def firstnameFieldV = issue.getCustomFieldValue(firstnameTypeCF)

if(firstnameFieldV.toString() == 'john')

{

    issue.setCustomFieldValue(outputField, 'john') 

    }

else

    {

     issue.setCustomFieldValue(outputField, 'Fail')

      }

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Tuesday in Marketplace Apps

If you’re an Atlassian app developer, you’ll want to know about Atlas Camp!

This September 6-7, hundreds of Atlassian App developers will flock to Barcelona Spain to build skills, discover product roadmaps, meet face-to-face with the Atlassian team, and learn how to extend t...

113 views 0 3
Read article

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