Forums

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

How to set help text in a Scriptrunner Behaviour that is performing other functions

Michelle Campbell January 31, 2023

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

1 answer

0 votes
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
February 6, 2023

Hi @Michelle Campbell

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:-

behaviour_config.png

I hope this helps to solve your question. :-)

Thank you and Kind regards,

Ram

 

 

Suggest an answer

Log in or Sign up to answer