Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Initialize behaviours

baptistemuller April 29, 2019

Hi everyone,

I use behaviours because I want to change the description of my issues in accordance of the Pre-Qualification (it's a custom field which is a Select List).

But i don't know why but my script don't work except when I comment on the part between number 1 and 2. I don't see errors and when I put it in the "Script Console" the part(between number 1 and 2) works.

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.*
import com.atlassian.jira.issue.fields.*

//1
//Issue and CustomField Manager
IssueManager issueManager = ComponentAccessor.getIssueManager()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
//Issue's Id
long issueId = underlyingIssue.getId()
//the current issue
Issue issue = issueManager.getIssueObject(issueId)
//CustomField Name
CustomField customField = customFieldManager.getCustomFieldObjectByName("Pre-Qualification")
//Value of the CustomField of the current issue
Object customFieldValue = customField.getValue(issue)
//2

def desc = getFieldById("description")

def bugValue = """
*For a Bug
*Navigateur / Brower* : _Chrome v70, IE10 ..._
*Terminal / Device* : iPhone X iOS 12.0, Sony Xperia XZ1, Window XX, Mac, IPad, ..
*Environnement / Environment:* _QA , Prod, ..._
*Pré-requis / Pre-requisites* :
*Etapes / Steps* :
# _..._
# _..._
# _..._

*Résultat attendu / Expected result* : _Description_
*Résultat obtenu / Actual result* : _Description_
*Systématique / Systematic ?* _Y / N_
""".replaceAll(/ /, '')

if (!underlyingIssue?.created && !underlyingIssue?.description) {
if(customFieldValue.getValue() == "Bug"){
desc.setFormValue(bugValue)
}
}

to sum up, when i put this script without the part 1->2 in the behaviour edit its work but all the choice in the Pre-Qualification List have the same description which is "bugValue". And when i comment-out the part, nothing work and i don't know why.

 

2 answers

1 accepted

0 votes
Answer accepted
baptistemuller May 2, 2019

i have tried this method:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.*

def desc = getFieldById("description")

def bugValue = """
*For a Bug
*Navigateur / Brower* : _Chrome v70, IE10 ..._
*Terminal / Device* : iPhone X iOS 12.0, Sony Xperia XZ1, Window XX, Mac, IPad, ..
*Environnement / Environment:* _QA , Prod, ..._
*Pré-requis / Pre-requisites* :
*Etapes / Steps* :
# _..._
# _..._
# _..._

*Résultat attendu / Expected result* : _Description_
*Résultat obtenu / Actual result* : _Description_
*Systématique / Systematic ?* _Y / N_
""".replaceAll(/ /, '')

IssueManager issueManager = ComponentAccessor.getIssueManager()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

//long issueId = 92341
long issueId = underlyingIssue.getId()
Issue issue = underlyingIssue
//Issue issue = issueManager.getIssueObject(issueId)

def preQualification = customFieldManager.getCustomFieldObject("customfield_10203")
String preQualificationValue = preQualification.getValue(issue)

if(preQualificationValue=="Bug"){
desc.setFormValue(bugValue)
}

and this one:

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

int preQualificationId = 10203
def preQualification = getFieldById("customfield_"+preQualificationId)
String preQualificationValue = preQualification.getValue()

def desc = getFieldById("description")

def bugValue = """
*For a Bug
*Navigateur / Brower* : _Chrome v70, IE10 ..._
*Terminal / Device* : iPhone X iOS 12.0, Sony Xperia XZ1, Window XX, Mac, IPad, ..
*Environnement / Environment:* _QA , Prod, ..._
*Pré-requis / Pre-requisites* :
*Etapes / Steps* :
# _..._
# _..._
# _..._

*Résultat attendu / Expected result* : _Description_
*Résultat obtenu / Actual result* : _Description_
*Systématique / Systematic ?* _Y / N_
""".replaceAll(/ /, '')

if(preQualificationValue == "Bug"){
desc.setFormValue(bugValue)
}

but nothing work, I don't understand why.

I think my issueId is not good.

Antoine Berry
Community Champion
May 2, 2019

Hi @baptistemuller , 

If you are willing to apply this behaviours when issue is being created right ? If so underlyingIssue object does not exist at this time yet, so I would not advise the first script.

What do the logs return if you use : 

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

int preQualificationId = 10203
def preQualification = getFieldById("customfield_"+preQualificationId)
String preQualificationValue = preQualification.getValue()

def desc = getFieldById("description")

def bugValue = """
*For a Bug
*Navigateur / Brower* : _Chrome v70, IE10 ..._
*Terminal / Device* : iPhone X iOS 12.0, Sony Xperia XZ1, Window XX, Mac, IPad, ..
*Environnement / Environment:* _QA , Prod, ..._
*Pré-requis / Pre-requisites* :
*Etapes / Steps* :
# _..._
# _..._
# _..._

*Résultat attendu / Expected result* : _Description_
*Résultat obtenu / Actual result* : _Description_
*Systématique / Systematic ?* _Y / N_
""".replaceAll(/ /, '')
log.error("preQualification field : " + preQualification)
log.error("preQualificationValue : " + preQualificationValue)

if(preQualificationValue == "Bug"){
log.error("setting the description...")
desc.setFormValue(bugValue)
}

Also, please provide a screenshot of your behaviours configuration.

Antoine 

Like baptistemuller likes this
baptistemuller May 2, 2019

hello @Antoine Berry , ty for your asnswer, i've put your script in the script console:

error :

Cannot get property 'customfield_10203' on null object

 

log:

2019-05-02 14:32:01,321 WARN [common.UserScriptEndpoint]: Script console script failed: java.lang.NullPointerException: Cannot get property 'customfield_10203' on null object

at com.onresolve.jira.groovy.user.FormField.getValue(FormField.groovy:203)

at com.onresolve.jira.groovy.user.FormField$getValue$2.call(Unknown Source)

at Script100.run(Script100.groovy:8)

 

this is my behaviours configuration:

Capture d’écran de 2019-05-02 14-40-13.png

i hope it will help you.

Baptiste

Antoine Berry
Community Champion
May 2, 2019

You should link the behaviours script to the Pre-Qualification field, not the Initializer, so it "listens" to any modification (therefore a it will detect when Bug is selected). 

Are you sure 10203 is the id of the PreQualification field ?

Antoine

Like baptistemuller likes this
baptistemuller May 2, 2019

how i can link them ? with a server-side script ? with a condition ?

and yes i'm sure for the customfield id.

baptistemuller May 2, 2019

i don't know if this can help you, but i have this log that i found in System/Audit Log:

Cofiguration:
<config name="TEST" description="TEST" use-validator-plugin="false" guideWorkflow="null"> <field id="customfield_10203" validator-script="" validator-class="" validator-method="" readonly="false" required="false" hidden="false" validator="server"/> <init id="__init__" validator-script="import com.onresolve.jira.groovy.user.FieldBehaviours&#10;import groovy.transform.BaseScript&#10;&#10;@BaseScript FieldBehaviours fieldBehaviours&#10;&#10;int preQualificationId = 10203&#10;def preQualification = getFieldById(&quot;customfield_&quot;+preQualificationId)&#10;String preQualificationValue = preQualification.getValue()&#10;&#10;def desc = getFieldById(&quot;description&quot;)&#10;&#10;def bugValue = &quot;&quot;&quot;&#10;*For a Bug&#10;*Navigateur / Brower* : _Chrome v70, IE10 ..._&#10;*Terminal / Device* : iPhone X iOS 12.0, Sony Xperia XZ1, Window XX, Mac, IPad, ..&#10;*Environnement / Environment:* _QA , Prod, ..._&#10;*Pré-requis / Pre-requisites* : &#10;*Etapes / Steps* :&#10;# _..._&#10;# _..._&#10;# _..._&#10;&#10;*Résultat attendu / Expected result* : _Description_&#10;*Résultat obtenu / Actual result* : _Description_&#10;*Systématique / Systematic ?* _Y / N_&#10;&quot;&quot;&quot;.replaceAll(/ /, '')&#10;log.error(&quot;preQualification field : &quot; + preQualification)&#10;log.error(&quot;preQualificationValue : &quot; + preQualificationValue)&#10;&#10;if(preQualificationValue == &quot;Bug&quot;){&#10;log.error(&quot;setting the description...&quot;)&#10;desc.setFormValue(bugValue)&#10;}" validator-class="" validator-method="run" validator="server"/> </config>
Antoine Berry
Community Champion
May 2, 2019

So it seems to be working fine. The issue is that you are using the script in the Initializer. This means the script is called when you are displaying a screen for the first time. 

In your case you need to map the script to the Pre-Qualification field, so that it is called each time you are changing this field value.

Just click on "Add server-side script" in the screenshot you have provided.

image.png

Like baptistemuller likes this
baptistemuller May 2, 2019

it works! thank you a lot for your help and your time !

Baptiste.

Antoine Berry
Community Champion
May 2, 2019

You are welcome ! Feel free to accept the answer if you are satisfied with it.

Also I would advise to add this condition 

if (!underlyingIssue && desc.getValue() == "") {
if(preQualificationValue == "Bug"){

so that 

  1. the description will update only if issue is being created (i.e. underlyingIssue is null)
  2. the description is empty. You do not want to erase the description if the user has been adding an input initially.

But of course this is up to you and your needs ! 

Antoine

Like baptistemuller likes this
baptistemuller May 2, 2019

ok thank you for your advice!!

Like Antoine Berry likes this
1 vote
Antoine Berry
Community Champion
April 29, 2019

Hi @baptistemuller ,

Make sure to map the behaviour to the Pre Qualification custom field and kindly try this script (replace the custom field id) : 

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

int preQualificationId = 12900
def preQualification = getFieldById("customfield_" + preQualificationId)
def preQualificationValue = preQualification.getValue()

def desc = getFieldById("description")

if (!underlyingIssue && desc.getValue() == "") {
if(preQualificationValue == "Bug"){
def bugValue = """
*For a Bug
*Navigateur / Brower* : _Chrome v70, IE10 ..._
*Terminal / Device* : iPhone X iOS 12.0, Sony Xperia XZ1, Window XX, Mac, IPad, ..
*Environnement / Environment:* _QA , Prod, ..._
*Pré-requis / Pre-requisites* :
*Etapes / Steps* :
# _..._
# _..._
# _..._

*Résultat attendu / Expected result* : _Description_
*Résultat obtenu / Actual result* : _Description_
*Systématique / Systematic ?* _Y / N_
""".replaceAll(/ /, '')
desc.setFormValue(bugValue)
}
}

Antoine

baptistemuller April 30, 2019

Thank you again @Antoine Berry bu I don't understand why but this doesn't work, the console don't make any error but nothing change.

Antoine Berry
Community Champion
April 30, 2019

The above script doesn't work ? There was a typo in the initial comment btw.

If I understood it right you want to set the description if "Bug" is selected ?

baptistemuller April 30, 2019

yes that's right and if I select "Task" in the Pre-Qualification for example it will be another description.

Antoine Berry
Community Champion
April 30, 2019

Added some logs in the script, what do you get ? 

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

int preQualificationId = 12900
def preQualification = getFieldById("customfield_" + preQualificationId)
def preQualificationValue = preQualification.getValue()
log.error("preQualificationValue : " + preQualificationValue)
def desc = getFieldById("description")
log.error("desc.getValue() : " + desc.getValue())
log.error("underlyingIssue : " + underlyingIssue)
if (!underlyingIssue && desc.getValue() == "") {
if(preQualificationValue == "Bug"){
def bugValue = """
*For a Bug
*Navigateur / Brower* : _Chrome v70, IE10 ..._
*Terminal / Device* : iPhone X iOS 12.0, Sony Xperia XZ1, Window XX, Mac, IPad, ..
*Environnement / Environment:* _QA , Prod, ..._
*Pré-requis / Pre-requisites* :
*Etapes / Steps* :
# _..._
# _..._
# _..._

*Résultat attendu / Expected result* : _Description_
*Résultat obtenu / Actual result* : _Description_
*Systématique / Systematic ?* _Y / N_
""".replaceAll(/ /, '')
desc.setFormValue(bugValue)
}
}
baptistemuller April 30, 2019
2019-04-30 09:54:30,622 WARN [common.UserScriptEndpoint]: Script console script failed:
java.lang.NullPointerException: Cannot get property 'customfield_12900' on null object
 at com.onresolve.jira.groovy.user.FormField.getValue(FormField.groovy:203) at com.onresolve.jira.groovy.user.FormField$getValue$2.call(Unknown Source) at Script667.run(Script667.groovy:8)
Antoine Berry
Community Champion
April 30, 2019

You need to replace 12900 with the id of your Pre-Qualification field

Like baptistemuller likes this
baptistemuller April 30, 2019

i think the double "if" condition don't allow the script to continu because when I comment it i can see the descrption in my issues

Suggest an answer

Log in or Sign up to answer