Scriptrunner behavior default text and validation error resets field completely

Christian Körner April 27, 2017

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

2 votes
Answer accepted
Aidan Derossett _Adaptavist_
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 28, 2017

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)
}
Christian Körner May 2, 2017

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.

Christian Körner May 2, 2017

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

Thanks!

Ian Bantilan June 12, 2017

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

Christian Körner June 13, 2017

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

Ian Bantilan June 13, 2017

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)

Christian Körner June 13, 2017

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
Peter Friberg March 19, 2019

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
TAGS
AUG Leaders

Atlassian Community Events