setting customfield value is not working

Tejashree Kotkar October 8, 2015

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
Answer accepted
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 8, 2015

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)
Tejashree Kotkar October 11, 2015

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

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 12, 2015

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?

Tejashree Kotkar October 12, 2015

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.

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 12, 2015

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

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.
October 13, 2015

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

AndrewB October 13, 2015

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

Jamie Echlin _ScriptRunner - The Adaptavist Group_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
October 13, 2015

actually customfield_10807 :-)

AndrewB October 13, 2015

*facepalm

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 13, 2015

Gah, sorry.

3 votes
Mark McCormack _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.
October 13, 2015

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

Vineela Durbha January 28, 2019

 I tried the same, but it is not setting the value.It is setting it to null

Any help on this 

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 28, 2019

That suggests that the field contains no data for that issue.

1 vote
Tejashree Kotkar October 14, 2015

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?

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.
October 15, 2015

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

Tejashree Kotkar October 15, 2015

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

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.
October 15, 2015

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

Tejashree Kotkar October 16, 2015

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

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 16, 2015

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.

Tejashree Kotkar October 20, 2015

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.

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 20, 2015

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.

Tejashree Kotkar November 5, 2015

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.

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 5, 2015

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.

Tejashree Kotkar November 6, 2015

Thanks for the answer :)

Nikhil Rote September 27, 2017

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.

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.
September 27, 2017

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.

Nikhil Rote October 10, 2017

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

      }

Pravin Awati August 21, 2019

This is not working for me it still showing the Null 

Can you please help?


Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events