How do I avoid overwriting previous data when setting a default value using a behavior?

I have a behavior that enters a bug template into the description field of the issue when it is of type "Bug". However, I want to avoid having people lose information if they go back and change the issue type to Bug after entering information.


STR Example:

  • Click "Create", create issue screen defaults to the current project, Task
  • Enter information including a description.
  • Realize this is a bug, switches from issue type Task to issue type Bug.
  • Behavior kicks in and replaces the description with the bug template.

I'm checking for a previously filled in form field (using getValue) but the value is returning null always. Is there a different method I should be using?

Code:

import com.onresolve.jira.groovy.user.FieldBehaviours
import static com.atlassian.jira.issue.IssueFieldConstants.*
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

if (getActionName() != "Create") {
	return // not the initial action, so don't set default values
}
def templateValue = """*Device / OS:*
*Test Account:*
*Precondition:*
*Steps to Reproduce:*
#
#
#
*Expected Result:*

*Observed Result:*

*Reproducibility:*"""

def descriptionField = getFieldById(DESCRIPTION)
def previousValue = (descriptionField.getValue() as String)?.trim();

if(previousValue == null || previousValue?.equals(templateValue)) {
	previousValue = "";
} else if(previousValue) {
	previousValue += "\n\n";
}

descriptionField.setFormValue(previousValue + templateValue)

5 answers

1 accepted

3 votes

You can't do this as an initialiser, because currently the form contents are not available in initialisers. Therefore you need to attach it to a field as in Jonny's example. There you can check whether the value is currently empty.

You can also use getUnderlyingIssue(), which will return null if this is the Create action (better than checking the action by name).

 

I've switched over to this, but it looks like modifying the field this way turns off inline editing. Even changing the behavior script to add

descriptionField.allowInlineEdit = true

Doesn't seem to fix the issue. Is there something I'm missing to keep inline editing enabled as a result of this behavior? 

That's deliberate. ScriptRunner automatically disables inline editing for fields that are marked as part of behaviours. The reason for this is that a behaviour can be complex enough that inline editing becomes unhelpful, even harmful in many use cases. Since inline editing can't make dependent fields editable, there's no way to cover one of the most common use cases for behaviours.

Sure, but is there any way to override that behavior? In this case the behaviour is not complex enough to warrent disabling inline field editing.

There's not a great solution for this problem. I'm not sure why allowInlineEdit = true is not working.

The problem is that the current form contents are not available in an initialiser, the rationale being that you can get these values from the issue in the database. The problem comes when you switch project or issue type, then the initialiser runs again.

I've created an issue for this: https://productsupport.adaptavist.com/browse/SRJIRA-2027

There's a related bug for allowInlineEdit that is still in "TODO".

https://productsupport.adaptavist.com/browse/SRJIRA-292

In experiments on a test server, allowInlineEdit works about half the time, but I've never had it work on our actual instance.

Odd. That script actually works for me. What version of JIRA and ScriptRunner are you using?

Here's some quick screenshots of how I configured it. Maybe you need to adjust the mapping?

Screen Shot 2016-09-07 at 1.33.10 PM.pngScreen Shot 2016-09-07 at 1.33.17 PM.png

I'm running as an initializer, would a validator be better? ScriptRunner 4.3.6, JIRA 7.1.2

Well, attaching the behaviour to the description field will seemed to make your script work, so... probably that's better than an initializer, for your use case? The initializers only run once when the form is loaded.

Initializers run when you switch types, the field validators run every time you switch on / off fields.  I'd love for the initializer to know what's in the form after it's been loaded but it appears that's not a thing it can do.

I've had to modify the code slightly to work bound to the description field instead (to prevent issues when switching back and forth between different issue types)

import com.onresolve.jira.groovy.user.FieldBehaviours
import static com.atlassian.jira.issue.IssueFieldConstants.*
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

if (getActionName() != "Create") {
    return // not the initial action, so don't set default values
}

def templateValue = """*Device / OS:*
*Test Account:*

*Precondition:*

*Steps to Reproduce:*
#
#
#

*Expected Result:*


*Observed Result:*


*Reproducibility:*"""

def descriptionField = getFieldById(DESCRIPTION)
def previousValue = (descriptionField.getValue() as String)?.trim();
if(previousValue?.length() <= 0) {
    descriptionField.setFormValue(templateValue)
}

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published 11 hours ago in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

35 views 0 3
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