Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

ScriptRunner Behaviour reseting select list value when read-only

Carlos David October 2, 2019

The following Behaviour is used to restrict the available options in a select list 'Fruit'based on the security levels available to the user. The value of the field is editable *only* on the Create Screen, set using a second simple Behaviour that isn't of interest here. 

Editing an Issue (i.e. the Edit Screen) correctly shows the Fruit select list (read-only) with the correct value selected. However upon clicking 'Update' or 'Cancel' the view issue page reloads and the Fruit value has changed to equal the first (top) entry in the select list. 

I don't believe this happened prior to our recent upgrades to SRunner 5.5.7-jira8 and to Jira 8.3.1.

Aside from manually re-setting the Fruit to the original value, can anyone advise on why this happened or how to load the select list options in a cleaner/safer way.

 

Thanks

 

=====================

import com.atlassian.jira.issue.security.IssueSecurityLevel
import com.atlassian.jira.issue.security.IssueSecuritySchemeManager
import com.atlassian.jira.issue.security.IssueSecurityLevelManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.customfields.option.Options
import com.onresolve.jira.groovy.user.FormField

IssueSecurityLevelManager issueSecurityLevelManager = ComponentAccessor.getComponent(IssueSecurityLevelManager.class)
def optionsManager = ComponentAccessor.getOptionsManager()

//Get security levels for which the *current user* is registered
ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def securityLevels = issueSecurityLevelManager.getAllSecurityLevelsForUser(currentUser)
def securityLevelNames = securityLevels*.name

def customFieldManager = ComponentAccessor.getCustomFieldManager()

//hard coded reference to Fruit Select List
def cField = customFieldManager.getCustomFieldObjectsByName("Fruit")[0]
def fieldConfig = cField.getRelevantConfig(getIssueContext())
Options options = optionsManager.getOptions(fieldConfig)

//log.debug "options: " + options.toString()
Map<String, Object> optionsMap = options.findAll {
it.value in securityLevelNames // all options you wish to display in the select list
}.collectEntries{
[
(it.optionId.toString()): it.value
]
}

// Get a pointer
FormField fruitSelectList = getFieldByName("Fruit")
fruitSelectList.setFieldOptions(optionsMap)

0 answers

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events