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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Set default field value to nothing

Edited

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

0 votes
Answer accepted

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.

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.

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

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

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

@Peter-Dave Sheehan 

 

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

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Apps & Integrations

🍻🍂Apptoberfest Update: Upcoming Virtual Events 🎉

Hello Community! I hope you've been enjoying the 🍂Apptoberfestivities🍂 (I know I have!) The event is heating up next week with a series of virtual events that we're calling the 🍻🍂Partner App ...

97 views 1 6
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you