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

Scriptrunner Behaviour - remove select list options but keep "None" option

Chris Sharpe
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!
October 5, 2022

I want to restrict a subtask's Project Specific Select Field custom field options based on other peer subtask's values in the same field. I'm not sure that PSSF custom field type matters here versus a standard select list, but wanted to mention it anyway.

The code below mostly works as expected. I can create a subtask and get all possible options (e.g. "A", "B", "C") for Target Release field. I choose "A" for this subtask. When I create a second subtask, I am presented with the full list of options minus the one I selected in the first subtask (i.e. "B", "C").

However, this is a required field. So normally the form would also present a "None" option as the selected value and thus force the user to make a choice here.  But with the behaviour active, "None" gets removed.  This basically makes a selection by default and also means other things don't trigger as they normally would on field change.

So I added the line to explicitly add "None" to the options list.  Now, it shows TWO None options.  So with my code to prune the choice list, I can either have no None value or two None values.  How do I get the pruned list plus a single default None entry (that's not a valid choice)?

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager


import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

def targetReleaseId = 18100 // Target Release - Project Specific Select Field
FormField targetRelease = getFieldById("customfield_" + targetReleaseId)

def parent
def parentIssue
def issueManager
def subTasks
def summary
def targetReleaseValue

summary = getFieldById("summary")
String currentSummary = summary.getFormValue() as String
targetReleaseValue = targetRelease.getValue() as String
parent = getFieldById("parentIssueId")
Long parentIssueId = parent.getFormValue() as Long
log.warn("Summary: " + currentSummary)
log.warn("Target Release: " + targetReleaseValue)
log.warn("Parent Id: " + parentIssueId)

issueManager = ComponentAccessor.getIssueManager()
parentIssue = issueManager.getIssueObject(parentIssueId)
subTasks = parentIssue.getSubTaskObjects()

if (!parent) {
log.warn("Not a subtask; returning")
return
}

// remove any Target Release value options that exist in other TR subtasks
def existingTargetReleases = []

subTasks.each {
log.warn("Subtask: " + it)

// This is cheating a bit, but we'll deal with summary field instead of
// the Project Specific Select Field "Target Release" since I couldn't get that
// to work properly inside this subTasks each loop
if (it.getIssueType().name == "Target Release Sub-task") {
log.warn("TR summary: " + it.summary)
// add existing target release names to list
existingTargetReleases.push(it.summary)
}
}

// set options list to be all targets minus existing targets; but keep None
def optionsManager = ComponentAccessor.getOptionsManager()
def targetReleaseField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(targetReleaseId)
def targetReleaseConfig = targetReleaseField.getRelevantConfig(getIssueContext())
def trOptions = optionsManager.getOptions(targetReleaseConfig)
def newTrOptions = [:]
newTrOptions.put("-1", "None") // want to keep the None option; BUG! but this gives two None options
newTrOptions += trOptions.findAll { !(it.value in existingTargetReleases) }.collectEntries {
[(it.optionId.toString()): it.value]
}
log.warn("All Target Release Options: " + trOptions)
log.warn("Existing Target Releases: " + existingTargetReleases)
log.warn("Allowable Target Releases: " + newTrOptions)
targetRelease.setFieldOptions(newTrOptions)

// Set summary to be the target release value
log.warn("Checking if setting summary value required")
if (issueContext.issueType.name == "Target Release Sub-task" && (!summary || targetReleaseValue != summary)) {
def newSummary = targetReleaseValue
summary = getFieldById("summary")
summary.setFormValue(newSummary)
log.warn("Summary now set to: " + summary.getFormValue())
log.warn("Set summary to be: " + newSummary)
} else {
log.warn("Did not set summary")
}


 

0 answers

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events