I want to add a value to select list field
Asked by aditya442:

Hi,

I have a target release field and would want to add value to it based on an issue creation. This field is not being used in the current issue that is being created but in another project. Any pointers?

example -

Project A with Issue Type Release. On creation of Release in Project A, the summary should get added to the Target Release (Project Specific Select List) so it can be used in Project B with Issue Type Target Release.

Any pointers would be appreciated?

Thanks,


Answers:
author: Florian Bonniec
body: Hi [@aditya442](/t5/user/viewprofilepage/user-id/4733679)

> String optionName = XXXX  
> CustomField customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject('customfield_xxxxxx')  
> Project project = ComponentAccessor.getProjectManager().getProjectByCurrentKey('XXXX')  
> OptionsManager optionManager = ComponentAccessor.getOptionsManager()  
> FieldConfigScheme fieldContexts = getFieldContext(customField, project)  
> FieldConfig fieldContxt = fieldContexts.getConfigs().get(fieldContexts.getConfigs().keySet().first())  
> if(fieldContxt){  
> if(!optionManager.getOptions(fieldContxt).getOptionForValue(optionName, null)){  
> optionManager.createOption(fieldContxt, null, null, optionName)  
> }  
> }

I did not test the script but the section that add the option is the one I use in my scripts

Regards

comments: author: aditya442
body: Thanks Florian for the response.I tried with the code you shared and it gives me an error. Please suggest.

![Screenshot 2024-08-12 at 10.37.45?AM.png](https://community.atlassian.com/t5/image/serverpage/image-id/341141i4762BD7FADE4FD94/image-size/large?v=v2&px=999 "Screenshot 2024-08-12 at 10.37.45?AM.png")

Thanks,

Aditya


author: Florian Bonniec
body: What is the error message ?


author: aditya442
body: This is the error I am receiving

![Screenshot 2024-08-13 at 5.49.19?PM.png](https://community.atlassian.com/t5/image/serverpage/image-id/341494iDC5F5AD4B4A45524/image-size/large?v=v2&px=999 "Screenshot 2024-08-13 at 5.49.19?PM.png")

This is my piece of code.

![Screenshot 2024-08-13 at 5.50.50?PM.png](https://community.atlassian.com/t5/image/serverpage/image-id/341495iBC4FDA151E9D6D58/image-size/large?v=v2&px=999 "Screenshot 2024-08-13 at 5.50.50?PM.png")

Thanks,

Aditya


author: aditya442
body: [@Florian Bonniec](/t5/user/viewprofilepage/user-id/495589) any suggestion?


author: Florian Bonniec
body: Hi

I just tested the script and fixed the issue
> import com.atlassian.jira.issue.fields.config.FieldConfig  
> import com.atlassian.jira.issue.fields.config.FieldConfigScheme  
> import com.atlassian.jira.issue.customfields.manager.OptionsManager  
> import com.atlassian.jira.project.Project  
> import com.atlassian.jira.issue.fields.CustomField  
> import com.atlassian.jira.component.ComponentAccessor
>
> <br />
>
> <br />
>
> <br />
>
> String optionName = "XXXXX"  
> CustomField customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject('customfield_XXXXX')  
> Project project = ComponentAccessor.getProjectManager().getProjectByCurrentKey('XXX')  
> OptionsManager optionManager = ComponentAccessor.getOptionsManager()  
> FieldConfigScheme fieldContexts = ComponentAccessor.getFieldConfigSchemeManager().getRelevantConfigScheme(project, customField)  
> if(fieldContexts){  
> FieldConfig fieldContxt = fieldContexts.getConfigs().get(fieldContexts.getConfigs().keySet().first())  
> if(fieldContxt){  
> if(!optionManager.getOptions(fieldContxt).getOptionForValue(optionName, null)){  
> optionManager.createOption(fieldContxt, null, null, optionName)  
> }  
> }  
> }

Regards


author: aditya442
body: Thanks a lot [@Florian Bonniec](/t5/user/viewprofilepage/user-id/495589) . It worked.


Tags: data-center

jira-data-center

scriptrunner