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
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.
def
cfConfig = cf.getRelevantConfig(issue)
value = ComponentAccessor.optionsManager.getOptions(cfConfig)?.
find
{ it.toString() ==
'Training'
}
Even using code above, it is not assign value to my custom field. And it is not giving any error in script.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
That probably means that the field does not exist for the project/issue-type you are trying to work with.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
this is wrong: getCustomFieldObjectByName("customFieldId=10807") it should be: getCustomFieldObjectByName("Name of custom field")
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
actually customfield_10807 :-)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Gah, sorry.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I tried the same, but it is not setting the value.It is setting it to null
Any help on this
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
That suggests that the field contains no data for that issue.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
depends on the type of customfield that you are comparing with Project1 etc, what type is it?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
so say cfProject is the project cf instance, it would be: if (issue.getCustomFieldValue(cfProject)?.value == "Project"1) {...}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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) }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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')
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This is not working for me it still showing the Null
Can you please help?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.