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:
- If "Shipping Request" is selected for the "reasonField":
- Only display the option "Shipment POC" in the "Deliver To" radio button
- Select "Shipment POC" in the "Deliver To" radio button
- Make the "recipientField" hidden and not required
- Set the help text of the "Deliver To" field to "xxx".
- If "Shipping Request" is NOT selected for the "reasonField":
- Display the options "Recipient" and "Inventory" in the "Deliver To" radio button
- Set the help text of the "Deliver To" field to "yyy".
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")
}