I'm looking for a way to set empty default value for select field.
I want to force the user to select an issue resolution consciously in particular project. I know about the option 'Disable defaults' on 'Resolutions' Jira administration page, but this makes resolution to have no default value for ALL projects in our Jira, when I need only single one.
The only way to acheive my goal I could think about - is to use Behaviours.
I already checked https://scriptrunner.adaptavist.com/ and merged several examples into a piece of code that changes default resolution selected in transition screen, but I couldn't find out how to completely disable default value selection or to make 'null' to be default for the Resolution field.
Can you help me with this?
P.S.: I used the following piece of code for default Resolution field value modification:
import com.atlassian.jira.component.ComponentAccessor
import static com.atlassian.jira.issue.IssueFieldConstants.RESOLUTION
if (getAction() != null) {
def constantsManager = ComponentAccessor.constantsManager
def defaultValue = constantsManager.resolutions.find { it.name == 'Duplicate' }
getFieldById(RESOLUTION).setFormValue(defaultValue.id)
}
That's because the setFormValue only works with existing values.
You must first replace the available options with your own list that includes a null option and then set that null option.
Try putting this in the initializer (not on the resolution or it will reset the value each time you select it).
import com.atlassian.jira.component.ComponentAccessor
import static com.atlassian.jira.issue.IssueFieldConstants.RESOLUTION
if (getAction() != null) {
def constantsManager = ComponentAccessor.constantsManager
def resolutionMap = constantsManager.resolutions.inject(["":"Select a resolution"]){map, res->
map[res.id] = res.name
map
}
def res = getFieldById(RESOLUTION)
res.setFieldOptions(resolutionMap)
res.setFormValue("")
}
Thanks a lot, it helped!
For the further readers: I modified the code a bit to avoid typecheck warnings and to make Jira check the selected value naturally, without additional validators.
import com.atlassian.jira.component.ComponentAccessor
import static com.atlassian.jira.issue.IssueFieldConstants.RESOLUTION
if (getAction() != null) {
def constantsManager = ComponentAccessor.constantsManager
def Map resolutionMap = [ null: "Please select" ]
constantsManager.resolutions.inject(
resolutionMap
) {
map, resolution ->
map[resolution.id] = resolution.name
return map
}
def resolutionField = getFieldById(RESOLUTION)
resolutionField.setFieldOptions(resolutionMap)
resolutionField.setFormValue(null)
}
When I use 'null' as key instead of empty string, Jira sees the field has no value and produces an error if field is required.
With empty string as a key it just leaves issue with 'Unresolved' resolution and you have to add validators to the transition.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Nice catch for the null vs emptyString for validations.
In the scenario where I had adapted this from is a text field converted to single select. In that case, emptyString would also fail a Field Required validation.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
If you need to combine this with a transition propety to reduce the resolution set (jira.field.resolution.include) and respect it, you will'll probably end up with something like this, i.e. read and react on result of getMetaAttributes(), where the transition properties are stored:
import com.atlassian.jira.component.ComponentAccessor
import static com.atlassian.jira.issue.IssueFieldConstants.RESOLUTION
def action = getAction()
if (action != null && underlyingIssue?.resolution == null) {
def resolutionField = getFieldById(RESOLUTION)
if (!resolutionField) return
String resolutionIdsString = action.getMetaAttributes().get("jira.field.resolution.include")
def isResolutionSetWhitelisted = (resolutionIdsString != null)
def whitelistedResolutionIds
if (isResolutionSetWhitelisted) {
whitelistedResolutionIds = resolutionIdsString.split(",")
}
def Map resolutionMap = [ null: "" ]
ComponentAccessor.constantsManager.resolutions.inject(
resolutionMap
) {
map, resolution ->
if (!isResolutionSetWhitelisted || resolution.id in whitelistedResolutionIds) {
map[resolution.id] = resolution.name
}
return map
}
resolutionField.setFieldOptions(resolutionMap)
resolutionField.setFormValue(null)
}
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This code defaults the resolution field back to 'null' regardless of the selection I have made...what am I doing wrong?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Do not add the Resolution field to the behaviour. Instead, add the code directly to the initializer.
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.