Forums

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

Set options to a Priority field using behaviours

Sir_ Anton Bykov
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
March 4, 2023

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:

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

final singleSelectListName = 'SingleSelectList'

[singleSelectListName].each { selectFieldName ->
    // Get the select field
    def selectField = getFieldByName("priority")

    // Getting select field options
    def selectCustomField = customFieldManager.customFieldObjects.findByName(selectFieldName)
    def selectConfig = selectCustomField.getRelevantConfig(issueContext)
    def selectOptions = ComponentAccessor.optionsManager.getOptions(selectConfig)

    // Filter select available options
    final selectAvailableOptions = selectOptions.findAll { it.value in ['10200', '2', '6', '10202'] }
    selectField.setFieldOptions(selectAvailableOptions)

    // Set the default values depending on select type
    if (selectFieldName == singleSelectListName) {
        def defaultValue = selectAvailableOptions.find { it.value in ['10200', '2', '6', '10202'] }
        selectField.setFormValue(defaultValue.optionId)
    }
}

2 answers

0 votes
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
March 8, 2023

Hi @Sir_ Anton Bykov

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 

0 votes
Asha Goyal
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.
March 4, 2023

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

Suggest an answer

Log in or Sign up to answer