Scriptrunner Behaviour: Evaluate description when user changes issue type

Based on the code sample on https://scriptrunner.adaptavist.com/latest/jira/behaviours-overview.html I have implemented template texts for whenever a user creates a new ticket and chooses "User Story" or "Bug" as an issue type.

Now there is the case where a user starts changing the description text and then realizes, that he has not yet selected the right issue type. So when he changes the issue type now, his custom description text vanishes and is replaced by the new template text. This only happens for issue types where a behaviour is defined for the description field, but not for other issue types.

Is there a way to prevent this?

def desc = getFieldById("description")
def defaultValue = """h3. IST-Verhalten
    …
    h3. Erwartetes SOLL-Verhalten
    …
    h3. Schritte zur Reproduktion
    # Schritt 1
    # Schritt 2""".replaceAll(/    /, '')
if (!underlyingIssue?.created && !underlyingIssue?.description) {
    desc.setFormValue(defaultValue)
}

3 answers

This widget could not be displayed.
Steven Behnke Community Champion Apr 11, 2017

I have this issue too, people's descriptions are overwritten when changing the issuetype, regardless of the underlyingissue condition. It evaluates to null when you're on the create screen.

This widget could not be displayed.

Hi flaimo,

Why the !underlyingIssue?.created condition? I mean all you need to check is if the description field has a value and if it hasn't ( that's what !underlyingIssue?.description does) then add a default one (desc.setFormValue(defaultValue))

This widget could not be displayed.

The example on your documentation page doesn't take into account the difference between newly created issues and existing issues being opened/edited.

Example:

  1. Click "Create"
  2. Select an issue type where a behavior inserts a template text into the description field.
  3. Delete the template text in the description field so that it is empty.
  4. Save the issue
  5. Open/Edit the issue → Not OK: The description field shows the template text again, even though the user explicitly deleted the text.

That is the reason why i do a check if the issue is just being created or if an existing one is edited.

flaimo 

try to use the getFieldScreen() in order to determine in which screen you are. 

Thanks for the tip, but !underlyingIssue?.created actually works fine for the corner case mentioned in my comment, but I still don't know how to deal with the situation in the original posting.

Hi @flaimo,

Did you find the solution to your issues?

 

Regards

Piotr

this is the code that we are currently using at it works fine:

 

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

//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("KSPECK")

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

//Initialize default templates and add them to an array
def defaultValue0          = """h3. Voraussetzungen
 * OS:
 * Browser:
 * Testumgebung/Branch:

h3. Schritte zur Reproduktion
 # Schritt 1
 # Schritt 2

h3. IST-Verhalten


h3. Erwartetes SOLL-Verhalten

…""".replaceAll(/    /, '')
def defaultValue1          = """*Als _<Benutzer in der Rolle>_ benötige ich _<eine Funktionalität>_, damit ich _<den geschäftlichen Nutzen habe>._*
----
h3. Akzeptanzkriterien
 # (zum Beispiel Tracking-Codes, WAI-Kriterien, Mobile/Tablet/Desktop-Anforderungen)
 #
 #

h3. Qualitätskriterien
 * (!) Readme und Dokumentation sind aktualisiert
 * (!) Neuer Code ist getestet
 * (!) Der Styleguide (Global, Projektspezifisch) ist aktualisiert

h3. Weitere Infos

…""".replaceAll(/    /, '')
def defaultValue2          = """*Als Entwickler benötige ich _<eine Funktionalität/Eigenschaft/Technologie>_, sodass _<Ziel/Nutzen/Begründung>._*
----
h3. Entwicklertestkriterien
h3. Weitere Infos

…""".replaceAll(/    /, '')
def defaultValue3          = """*Als _<Benutzer in der Rolle>_ benötige ich _<eine Funktionalität>_, damit ich _<den geschäftlichen Nutzen habe>._*
----
h3. Akzeptanzkriterien
 * (grobe Akzeptanzkriterien von denen sich normalerweise Stories, Spikes und Tasks ableiten)
 *
 *

h3. Weitere Infos


----
h3. DoD-Checkliste (PO)
 * Gibt es ein Projekt-Ticket mit dem der Epic verlinkt werden kann?""".replaceAll(/    /, '')
def defaultValue4          = """h3. Akzeptanzkriterien
 * Welche Unklarheiten soll aus dem Weg geräumt werden?
 * Welche Aktionen sollen basierend auf dem Outcome durchgeführt werden?
 * Ist die Recherchearbeit Timeboxed?

h3. Weitere Infos

…""".replaceAll(/    /, '')
def defaultValue5          = """h3. Beschreibung


h3. Qualitätskriterien
 * (!) Readme und Dokumentation sind aktualisiert
 * (!) Neuer Code ist getestet
 * (!) (Frontend) Der Styleguide (Global, Projektspezifisch) ist aktualisiert""".replaceAll(/    /, '')
def defaultValue6          = " "
def defaultArray           = [defaultValue0, defaultValue1, defaultValue2, defaultValue3, defaultValue4, defaultValue5]

//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 == "" || currentDesc == " ")
{
    //Given the current issueType ID, make a decision of which default to use
    switch(currentIssueType.getValue())
    {
        case issueType.find {it.name == "Defect"}?.id:
            desc.setFormValue(defaultArray[0])
            break
        case issueType.find {it.name == "User Story"}?.id:
        case issueType.find {it.name == "Improvement"}?.id:
            desc.setFormValue(defaultArray[1])
            break
        case issueType.find {it.name == "Dev Improvement"}?.id:
            desc.setFormValue(defaultArray[2])
            break
        case issueType.find {it.name == "Epic"}?.id:
            desc.setFormValue(defaultArray[3])
            break
        case issueType.find {it.name == "Spike"}?.id:
            desc.setFormValue(defaultArray[4])
            break
        case issueType.find {it.name == "Technical sub-task"}?.id:
            desc.setFormValue(defaultArray[5])
            break
        default:
            desc.setFormValue(defaultArray[6])
    }
}

Screen Shot 2018-06-15 at 11.11.37.png 

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Aug 22, 2018 in Marketplace Apps

How a Marketplace app tech team is achieving gender diversity

Hello! My name is Genevieve Blanch, and I'm the Marketing Manager at RefinedWiki, creators of apps to give teams the tools to customize Atlassian platforms. Currently, 44% of the tech team at Re...

425 views 3 17
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