How to create default template using "Initialisers" in Script Runner add-on?

Michał Gawlik March 5, 2016

I have got question about "Initialisers" feature in Script Runner add-on.

I want to have a default value/template in one project for the Description field, but keep it empty for the rest of projects. But when I click on "Create" button, a template appears and later I change the project from drop-down list, the template is not cleaned up.
It's working but, but only when I create explicitly rule ("empty template") for the rest projects.
Every time, when new project is created, I need to remember about that. Isn't any better solution for that?

Thank you in advance for help!

Michal

 

def desc = getFieldById("description")
def defaultValue = """h2. How to reproduce
    * step 1
    * step 2
    h2. Expected Result
    The widget should appear
    h2. Actual Result
    The widget doesn't appear""".replaceAll(/    /, '')
if (! underlyingIssue?.description) { 
    desc.setFormValue(defaultValue)
}

2 answers

1 accepted

1 vote
Answer accepted
JamieA
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.
March 6, 2016

It can't really know what it's done, in order to undo it all when you change projects. The user may have made further edits and would not expect it to be wiped out.

In my experience, most end-users, most of the time, generally create issues in one project.

Michał Gawlik March 6, 2016

Ok, thank you for the answer.

Terry Beavers May 23, 2016

Hi @Jamie Echlin [Adaptavist],

Is it possible to templatize a custom field and use "underlyingIssue" to check if the custom field is empty?

I have used the code above to create a standard template for the Description field and it works great, but can't seem to figure out how to do the same for a custom field.

For example:

def cf = getFieldById("customfield_10013")
def defaultValue = """Blah blah blah
    """.replaceAll(/    /, '')
if (! underlyingIssue?.(CustomField "customfield_10013")) {
cf.setFormValue(defaultValue)
}

Thanks in advance for your support,

Terry

JamieA
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 26, 2016

Yes, but use CustomFieldManager to get the CF value from the issue. 

Terry Beavers May 26, 2016

Hi @Jamie Echlin (Adaptavist), please forgive my ignorance on this as I am not a developer, so still having some issues with getting the script to work for a custom field.

I have added the following and tried a few different things with underlyingIssue to check if the custom field is populated, but still can't get it to work.  :|

import com.atlassian.jira.issue.CustomFieldManager
def cf = getCustomFieldValue("customfield_10013")

Any additional help would be gratefully appreciated.

Thanks for your support,

Terry

Terry Beavers June 2, 2016

Hi @Jamie Echlin (Adaptavist), just wanted to check back with you to see if you had any additional input on how to resolve this issue with templatizing a custom field as the few things I tried did not work.

Thanks for your support,

Terry

0 votes
Andres Fuentes August 10, 2016

@Terry Beavers, Try This:

import com.atlassian.jira.issue.CustomFieldManager
def cf = getFieldById("customfield_10013")
def defaultValue = """Blah blah blah
    """.replaceAll(/    /, '')
if (! underlyingIssue?.description) {
cf.setFormValue(defaultValue)
}

Apparently ".description" just pulls the value of the custom field. I think they could have avoided some confusion if the example they used wasn't the "Description" field.

Terry Beavers August 16, 2016

That did the trick!  Thank you @Andres Fuentes!

import com.atlassian.jira.issue.CustomFieldManager
def cf = getFieldByName("Steps to Reproduce")
def defaultValue = """*+Steps to Reproduce+*
*+Actual Results+*
*+Expected Results+*
    """.replaceAll(/    /, '')
if (! underlyingIssue?.description) {
cf.setFormValue(defaultValue)
}

I do have another odd scenario I discovered while testing though that hopefully you can shed some light as well.

This particular behaviour script is configured to execute against the following two issue types, Bug and Hot Fix.

The problem I have encountered is if the user enters data in the Steps to Reproduce field, but then changes the issue type to the other issue type that is supported with the script, the current data in the field is wiped out and replaced with a new Steps to Reproduce template.

For example, I go to create a new issue as a Bug and populate the Steps to Reproduce field with some additional data using the template.  I then change the issue type to Hot Fix before submitting the ticket and the data in the field is wiped out and replaced with a new Steps to Reproduce template.

On the other hand, if I go to create a new issue as a Bug and populate the field with some additional data using the template and then change the issue type to Improvement before submitting the ticket, the data remains in tact as expected, but if I then change the issue type back to Bug or Hot Fix before submitting the ticket, the data in the field is wiped out and replaced with a new Steps to Reproduce template.

It seems like the script is reinitializing when the issue type is changed to a supported issue type, but only during create, not during edit.

Any thoughts, ideas or suggestions?

JIRA version is 7.0.10 and ScriptRunner version is 4.3.5

Thanks for your support!

Andres Fuentes August 16, 2016

Just replying with some initial thoughts... I have yet to investigate this. 

So in researching this last week I found some people were talking about this an issue similar to what you're mentioning. I think you may have to add some additional logic.

Something like:

if (!underlyingIssue?.created && !underlyingIssue?.description) {
    desc.setFormValue(defaultValue)
}

This would verify that the issue is being created and not moved.

Suggest an answer

Log in or Sign up to answer