Field Behavior - Initialiser Function

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

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

 

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

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Monday in Confluence

Organizing your space just got easier - Page Tree Drag & Drop is here

Hi Community! I’m Elaine, Confluence Product Manager. You may have read my earlier post about page tree in space navigation sidebar. I'm excited to share another improvement that helps you organize ...

94 views 3 4
Join discussion

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