Forums

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

Behaviour Script is not working as Intended

shrikant maheshwari March 31, 2022

Hi All,

I have tried using behaviour and putting my script in the Initialiser and in the server script as well for the corresponding field but the script is not working as Intended

Requirement -

Based on the "Project category" ,  i need to check "Issue type" and if the issue type is "BUG " then based on that I need to make a "Root Cause" mandatory while creating the issue and editing the issue as well .

At the time of create the scripts functions well but at the time of Editing the issue and changing the Issue type from BUG to other or VICE-VERSA the "Root Cause" field does not become mandatory or non mandatory on the basis of issue type?

Please find the below scripts-

I am using the same script at both the places.

 

NOTE - The issue what i have observed is when i ma editing the Issue type its ID is not getting changed and it still returns the initial ID of the issue type and hence the IF condition is failing.


----------------Initialiser and Server Script on the Field Issue type ----------------

 

import com.atlassian.jira.component.ComponentAccessor
import groovy.transform.BaseScript
import static com.atlassian.jira.issue.IssueFieldConstants.*

def field = getFieldByName("Issue Type")

def value1 =field.getValue() as String

def pdv = getFieldById("customfield_11104")

log.warn("Value while editing the issue: "+value1)

if(underlyingIssue.getProjectObject().getProjectCategory()){

if(underlyingIssue.getProjectObject().getProjectCategory().getName().toString().startsWith("NED")){

if (value1 == "10102"){
log.warn('Issue type is BUG at the time of edit')

pdv.setRequired(true)

log.warn("Root cause: ")

}
else
{
pdv.setRequired(false)

log.warn('Issue type is not the BUG at the time of edit')
}
}

else {

pdv.setRequired(false)
log.warn('Project category is not NED at the time of edit')

}
}

 

 

 

 

1 answer

0 votes
Antoine Berry
Community Champion
April 1, 2022

Hello @shrikant maheshwari ,

I tried having one behaviours script linked to the issue type field, and it worked.

Are you using only one script ? Do you have the issue type field on the edit screen ?

shrikant maheshwari April 1, 2022

i am using the same script at both the places. yes i do have the Issue type field on the edit screen .

 

The problem what i have observed is that when i EDIT the issue and change the Issue type from say task to BUG then the field "Root cause" does not become mandatory as i could see in the logs the ID of Issue type is still of TASK id did not changed to BUG hence the condition is failing.

Antoine Berry
Community Champion
April 1, 2022

Ok, so you only face the issue when 

  • you are editing the issue after it has been created
  • you are changing the issue type from any issue type to Bug

Is that correct ? If the issue type is already Bug, the script is working correctly ?

shrikant maheshwari April 4, 2022

yes absolutely correct .

So when I am changing the Issue type from say Task to BUG the field "Root cause" does not become mandatory and when i am changing the issue type from BUG to say task then the field is not becoming non-mandatory

Like Antoine Berry likes this
shrikant maheshwari April 4, 2022

@Antoine Berry 

Could you help in fixing this ?

Antoine Berry
Community Champion
April 6, 2022

Hi @shrikant maheshwari ,

I think because the object underlyingIssue does not exist at creation, there might be an issue with your script.

I think you should separate cases when the issue is created and is not, i.e. : 

if (!underlyingIssue) {
def field = getFieldByName("Issue Type")
def issueTypeValue = field.getValue() as String
log.warn("issue not created, issue type id is : " + issueTypeValue)
}
else {
def field = getFieldByName("Issue Type")
def issueTypeValue = field.getValue() as String
log.warn("issue type created, issue type id is : " + issueTypeValue)
}

This always returns the right id on my instance.

Suggest an answer

Log in or Sign up to answer