I have a Scriptrunner Behaviour that I want to do several things with, and since I'm a newbie I've merged several scripts that I've found online into this embarrassment below. Everything works besides for setting the Help Text - I had a behaviour for only setting the Help Text and it was working, but when I started messing around with this behaviour for the other requirements, the Help Text one stopped working, so I thought merging them into one might fix it, but it hasn't. Apologies in advance for the convoluted mess of a script - still learning!
Here is what I'm trying to do with the script below:
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
def reasonField = getFieldById(getFieldChanged())
def cf = getFieldByName("Deliver to")
def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cfField = customFieldManager.getCustomFieldObject(cf.getFieldId())
def cfConfig = cfField.getRelevantConfig(getIssueContext())
def cfOptions = optionsManager.getOptions(cfConfig)
def recipientField=getFieldById("customfield_11916")
def selectedOption = reasonField.getValue() as String
def isShippingSelected = selectedOption == "Shipping Request"
if(isShippingSelected){
def optionToSelectA = cfOptions.find { it.value == "Shipment POC" }
cf.setFormValue(optionToSelectA.optionId)
def cfB = cfOptions.findAll { it.value in ["Shipment POC"] }.collectEntries { [ (it.optionId.toString()) : it.value ] }
cf.setFieldOptions(cfB)
recipientField.setHidden(true)
recipientField.setRequired(false)
cf.setHelpText("xxx")
}
else {
def cfA = cfOptions.findAll { it.value in ["Recipient","Inventory"] }.collectEntries { [ (it.optionId.toString()) : it.value ] }
cf.setFieldOptions(cfA)
cf.setHelpText("yyy")
}
For your requirement, you could try something like this:-
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours behaviours
def sampleList = getFieldById(fieldChanged)
def sampleListValue = sampleList.value.toString()
def radioButton = getFieldByName('Radio Button')
def customFieldManager = ComponentAccessor.customFieldManager
def optionManager = ComponentAccessor.optionsManager
def radioButtonCustomField = customFieldManager.getCustomFieldObject(radioButton.fieldId)
def availableConfig = radioButtonCustomField.getRelevantConfig(issueContext)
def radioButtonOptions = optionManager.getOptions(availableConfig)
def availableOptions = ['Shipment POC', 'Recipient', 'Inventory']
def optionToSelect = radioButtonOptions.find { it.value in availableOptions }
def radioButtonMap = radioButtonOptions.findAll { it.value in availableOptions }.collectEntries { [ (it.optionId.toString()) : it.value ] } as Map
radioButton.clearHelpText()
if (sampleListValue == 'Option 1') {
availableOptions = ['Shipment POC']
optionToSelect = radioButtonOptions.find { it.value == availableOptions.first() }
radioButtonMap = radioButtonOptions.findAll { it.value in availableOptions }.collectEntries { [ (it.optionId.toString()) : it.value ] } as Map
radioButton.setHelpText('Select the Shipment POC option')
} else if (sampleListValue in ['Option 2', 'Option 3', 'Option 4']) {
availableOptions = ['Recipient', 'Inventory']
optionToSelect = radioButtonOptions.find { it.value in availableOptions }
radioButtonMap = radioButtonOptions.findAll { it.value in availableOptions }.collectEntries { [ (it.optionId.toString()) : it.value ] } as Map
radioButton.setHelpText('Select either the Recipient or Inventory option')
}
radioButton.setFormValue(optionToSelect.optionId)
radioButton.setFieldOptions (radioButtonMap)
Please note that the sample working code above is not 100% exact to your environment. Hence, you will need to make the required modifications.
For this behaviour to work, you must create a Server-Side Behaviour configuration for the List you are selecting from.
Below is a screenshot of the Behaviour configuration:-
I hope this helps to solve your question. :-)
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.