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

 

Suggest an answer

Log in or Join to answer
Community showcase
Teodora [Botron]
Published Thursday in Marketplace Apps

Jira Inferno: The Nine Circles of Jira Administration Hell

If you spend enough time as a Jira admin - whether you are managing a single, mid-sized instance, a large enterprise one or juggling multiple instances at once - you will eventually find yourself in ...

274 views 0 12
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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot