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

Field Behavior - Initialiser Function

David Jansson May 8, 2017

We are using the Initialiser Function in behavior to set a default template to the description field.

The initialiser is re-run if the Validation fails, for instance missing required fields, the description field is reset and information added by the reporter is over written.

New Fug? We didn't see this behavior in earlier versions of the plugin.

System Info:
JIRA : 7.3.5
scriptunner: 5.0.1

 

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
1 vote
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.
May 9, 2017

This is a known issue with behaviors (specifically initializers) that we are currently dealing with. Instead, try creating a field behavior on the description field. A simple example would look something like this:

def desc = getFieldById("description")

def defaultValue="Your Default"

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

For a more complex example, take a look at this script that is used to switch the default template based on the current issue-type. So the field behavior will be attached to the issue-type field:

import com.atlassian.jira.component.ComponentAccessor

//set necessary component managers
def projectManager = ComponentAccessor.getProjectManager()
def issueTypeSchemeManager = ComponentAccessor.getIssueTypeSchemeManager()

//grab current values/fields
def currentIssueType = getFieldById("issuetype")
def desc = getFieldById("description")
def currentDesc = desc.getValue()
def project = projectManager.getProjectByCurrentKey("YOUR PROJECT KEY")

//get all of the issue for your project
def issueType = issueTypeSchemeManager.getIssueTypesForProject(project)

//Initialize default templates and add them to an array
def defaultValue1 = "Default Text A"
def defaultValue2 = "Default Text B"
def defaultValue3 = "Default Text C"
def defaultArray = [defaultValue1, defaultValue2, defaultValue3]

//Enter if the current description is one of the other defaults
//Or if the description field is empty
//This ensures that the description field is left unchanged if the user has already typed something
if(defaultArray.contains(currentDesc) || currentDesc == "")
{
    //Given the current issueType ID, make a decision of which default to use
    switch(currentIssueType.getValue())
    {
        case issueType.find {it.name == "Bug"}?.id:
            desc.setFormValue(defaultArray[0])
            break
        case issueType.find {it.name == "New Feature"}?.id:
            desc.setFormValue(defaultArray[1])
            break
        //Continue with more cases or set a default
        default:
            desc.setFormValue(defaultArray[2])
    }
}

 

Frank Winkler September 3, 2018

Hi @Aidan Derossett [Adaptavist]

we have the same use case... We use a similar script, that is mapped to the issue type field. In normal "Create" screen this works fine. But: there is another "create issue" behavior: some times (for example if one right clicks the "Create" button and chooses "Open in new tab") there are two screens: first one to select project and issue type and second one to enter the issue data. In this case the script mapped to "Issue type" won't run - you need the Initialiser function. Unfortunately this Initialiser function runs in normal "Create" screen many times (at least after "Issue type" changed) and is not able to get the value of "Description" using getFieldById("description").getValue() - this always returns NULL and so the "Description" will always be replaced by a template.

Thx, best regards!

Frank

TAGS
AUG Leaders

Atlassian Community Events