Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Setting a default for a customfiled which user should be able to modify when required?

Raj Kumar July 26, 2019

Hi,

I have custom field "Location"(Single select Type) for which I am able to display required options in the dropdown but I am able to set a default value to the field but user is not able to modify when required . Below is the script I am using. Can someone help me fix this.

Your help is highly appreciated.

***************************************************************************************************

import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()

def Location = getFieldById("customfield_13943")


// Get access to the required custom field and options managers
def customField = customFieldManager.getCustomFieldObject(Location.getFieldId())
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)
def optionToSelect = options.find { it.value == "United States" }
def optionsMap = options.findAll {
it.value in ["International",
"United States"] // list of options you want to show
}.collectEntries {
[
(it.optionId.toString()): it.value
]
}
Location.setFieldOptions(optionsMap)
Location.setFormValue(optionToSelect.optionId)

 

Thanks,
Raj

2 answers

0 votes
Andrew
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.
July 26, 2019

Maybe no need make map?
https://scriptrunner.adaptavist.com/5.4.47/jira/behaviours-api-quickref.html#_formfield_setfieldoptions_iterable

formField.setFieldOptions(options.findAll {it.value in ['foo', 'bar']})

Raj Kumar July 29, 2019

Hi,

Still none is shown I want to get rid of it and want my first option to be shown instead of none.

Thanks,
Raj

0 votes
Andrew
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.
July 26, 2019

Hi @Raj Kumar ,

My similar example:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.manager.OptionsManager

def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject("customfield_id")
def cFieldValue = originalIssue.getCustomFieldValue(cField)
def optionsManager = ComponentAccessor.getComponent(OptionsManager)

def fieldConfig = cField.getRelevantConfig(issue)
def option = optionsManager.getOptions(fieldConfig).getOptionForValue("Value", null)

issue.setCustomFieldValue(cField, option)

 

B.R.

Raj Kumar July 29, 2019

This is not working as required.

Thanks,
Raj

Suggest an answer

Log in or Sign up to answer