I'm trying to set options to a Priority field using behaviours. I need to set High, Critical, Regular and Low priority and do not change the priority scheme for project.
I am having trouble getting my code to work.
Here's my current behaviour script:
Your approach doesn't seem to be correct.
I first noticed in your code that you are trying to use the SingleSelectList to determine the priority.
For this to work, you ensure that a Server-Side Behaviour is configured for that Single Select List, and second, the Single Select List is initiated as:-
def sampleList = getFieldById(fieldChanged)
The fieldChanged option is required to ensure that the Behaviour will only trigger when a change is made to that particular field. In this case, the Single Select List.
The second problem I noticed in your code is that you are invoking the Priority field using getFieldByName. This will not work.
The Priority field is a System Field, and to invoke a System Field, you need to use getFieldById as shown below:-
import static com.atlassian.jira.issue.IssueFieldConstants.PRIORITY
...
...
def priority = getFieldById(PRIORITY)
Below is a complete sample working Behaviour code for your reference:-
import static com.atlassian.jira.issue.IssueFieldConstants.PRIORITY
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours behaviours
def sampleList = getFieldById(fieldChanged)
def sampleListValue = sampleList.value.toString()
def priority = getFieldById(PRIORITY)
priority.readOnly = true
if (sampleListValue == 'Option 1') {
priority.setFormValue(1) // highest
} else if (sampleListValue == 'Option 2') {
priority.setFormValue(2)
} else if (sampleListValue == 'Option 3') {
priority.setFormValue(3)
} else if (sampleListValue == 'Option 4') {
priority.setFormValue(4)
}
Please note that the sample code above is not 100% exact to your environment.
The priority field value is set using the Integer value in the sample code above. So for the Highest priority, it is 1, Mid-Leve, it is 2 and so on. This is dependent on the priority values set in your environment.
I hope this helps solve your question. :-)
Thank you and Kind regards,
Ram
Hello @Sir_ Anton Bykov
The script you provided seems to be setting options for a SingleSelectList field rather than the Priority field. We recently worked for similar requirement, if that can help you.
final priorityOptions = [
['name': 'High', 'id': '1'],
['name': 'Critical', 'id': '2'],
['name': 'Regular', 'id': '3'],
['name': 'Low', 'id': '4']
]
def priorityField = getFieldById("priority")
def optionsManager = ComponentAccessor.optionsManager
// Get the current priority values for the project
def prioritySchemeManager = ComponentAccessor.prioritySchemeManager
def priorityScheme = prioritySchemeManager.getDefaultPriorityScheme()
def priorityValues = priorityScheme.getPriorityValues()
// Update the priority values with the options we want
priorityOptions.each { option ->
if (!priorityValues.find { it.getName() == option.name }) {
// Add the option to the priority scheme if it doesn't exist
def newPriority = prioritySchemeManager.createPriority(option.name, option.id as Integer, null)
prioritySchemeManager.addPriorityToScheme(newPriority, priorityScheme)
}
}
// Get the updated priority values for the project
priorityValues = priorityScheme.getPriorityValues()
// Filter the priority values to only include the options we want
def availableOptions = priorityValues.findAll { it.getName() in ['High', 'Critical', 'Regular', 'Low'] }
// Set the field options
priorityField.setFieldOptions(optionsManager.getOptions(availableOptions.collect { it.getId() as String }))
// Set the default value
priorityField.setFormValue('3')
Thanks
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.