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

Scriptrunner behavior default text and validation error resets field completely

We are using the scriptrunner behavior plugin to set the default text of the description (so that the user has a template to fill out) when opening the create issue screen. Everything works as expected, however an issue we are facing is the scenario when a validation error occurs when clicking "Create" in the dialog.

The validation error gets displayed however the description is set to the default text again, losing all previously entered information.

The code we are using is the following:

import com.atlassian.jira.component.ComponentAccessor

import static com.atlassian.jira.issue.IssueFieldConstants.*

if (getActionName() != "Create Issue") {
    return 
}

def desc = getFieldById("description")

def defaultValue = "SOME DEFAULT VALUE''

desc.setFormValue(defaultValue)

1 answer

1 accepted

Are you using an Initializer function? If you are, I would instead create a behavior directly on the Description field with a script similar to this:

def desc = getFieldById("description")

def defaultValue="Your Default"

if(!desc.getValue())
{
    desc.setFormValue(defaultValue)
}

Thanks for the answer, but how do I create a behavior directly on the description field?

I tried this by "Adding" the description field to the behavior and then adding a server side script with the snippet you provided. However this does unfortuantly not work.

Ok, I figured it out. I forgot to set a condition when this script should be run.

Thanks!

How did you do it? I have same situation..

I added a condition that the script should only be run when the workflow step is "Create issue" (the first workflow step in my workflow).

Is your validation in the behaviors plugin or in the field configuration? It still did not work for me. I still get my fields reset when validatioin fails during create issue (e.g. missing component, required in field configuration)

Behaviours

  • Add behaviour (name it)
  • Click "Fields" in newly created behaviour
  • Select your Guide Workflow
  • Add the description field to the relevant fields
  • Add the server side script
  • def desc = getFieldById("description")
    
    def defaultValue = "SOME TEXT HERE"
    
    if (!desc.getValue()) { 
        desc.setFormValue(defaultValue)
    }
  • add condition:
    • when "workflow action" - select your action

In this case, user input will be erased if a field is required in the field configuration (but not if set as required in behaviours script):

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()

def descTemplateField = getFieldById(getFieldChanged())
def desc = getFieldById("description")
def customField = customFieldManager.getCustomFieldObject(descTemplateField.getFieldId())
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)
def epicOption = "User Epic"

def optionsMap = options.findAll {
it.value in [epicOption] // list of options you want to show
}.collectEntries {
[
(it.optionId.toString()) : it.value
]
}
optionsMap.put ("-1", "None")
descTemplateField.setFieldOptions(optionsMap)

def epicTemplate = "Template Text"


if (descTemplateField.getValue() == null ) {
desc.setFormValue("")
}
else if (descTemplateField != null && descTemplateField.getValue() == epicOption) {
desc.setFormValue(epicTemplate)
}

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published Apr 03, 2019 in Marketplace Apps

How to estimate projects more accurately?

Gaining insight into your team’s effectiveness and choosing the right estimation technique.   Estimating projects is hard. It is because people have the tendency to underestimate the time thin...

296 views 2 9
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you