Set default field value to nothing

DenKoren
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!
April 24, 2019

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)
  }

2 answers

1 accepted

1 vote
Answer accepted
Peter-Dave Sheehan
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.
April 24, 2019

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("")
}
DenKoren
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!
April 25, 2019

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.

Peter-Dave Sheehan
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.
April 25, 2019

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.

Petr Papousek
Contributor
January 3, 2020

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)
}

 

Like # people like this
cgadade
Contributor
February 12, 2020

Hi @DenKoren ,

   How can we apply the same for Project field.as We want "please select a project" in project drop down while creating an issue. Thanks in advance

ghadeer raad September 12, 2020

@Peter-Dave Sheehan  please how i can change the value of priority to character not only number ?

0 votes
Judah
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.
November 5, 2020

@Peter-Dave Sheehan 

 

This code defaults the resolution field back to 'null' regardless of the selection I have made...what am I doing wrong? 

Rahul Savaikar
Contributor
October 25, 2021

Do not add the Resolution field to the behaviour. Instead, add the code directly to the initializer.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events