Scriptrunner Behaviour: Evaluate description when user changes issue type

flaimo February 11, 2016

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

5 answers

1 vote
Steven F Behnke
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.
April 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.

0 votes
Sílvia Ribeiro
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
July 30, 2021

Hi!

I have this problem 

'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.'

 

But, in our case, the customer description vanish, and the fields stays empty. 

How can I fix that?

Thanks!

0 votes
Veronica Kolstad December 27, 2018

I tried Flaimo's code to determine whether the Description field is not empty when still on the Create screen, but it's not working for me, perhaps because the goals are different. 

We are populating the typical user story format into our stories and bugs via Behaviours when users start the Create process, but if the user updates the field with their user story details and then updates the type or project, the new text is wiped out with the default user story. 

How can I get Behaviours to determine that the field is not actually null on the screen before the issue is created? 

0 votes
flaimo February 17, 2016

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.

Thanos Batagiannis [Adaptavist]
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.
February 24, 2016

flaimo 

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

flaimo February 26, 2016

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.

Piotr Gdaniec
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
June 15, 2018

Hi @flaimo,

Did you find the solution to your issues?

 

Regards

Piotr

flaimo June 15, 2018

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 

0 votes
Thanos Batagiannis [Adaptavist]
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.
February 17, 2016

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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events