Forums

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

Script Runner behaviour isn't working on Epic link field

Priyanka Nimbalkar July 13, 2022

Hi Team , 

I am trying to achieve this behaviour using script runner on epic link field to populate other fields based on the value selected in epic link , but somehow it isn't getting reflected . This same behaviour is working on Epic Name  fine. 

I have attached a sample code for the same:

Code :

def textField = getFieldByName("Epic Link")
def selectField = getFieldByName("XYZ")
def selectField2 = getFieldByName("ABC")
def selectField3 = getFieldByName("MNO")

def textFieldValue = textField.getValue().toString()

if(textFieldValue.equalsIgnoreCase("Non-Portfolio")) {
selectField.setFormValue("Non-Portfolio")
selectField2.setFormValue("Non-Portfolio")
selectField3.setFormValue("Non-Portfolio")
}

else if(textFieldValue == "") {

selectField.setFormValue(null)
selectField2.setFormValue(null)
selectField3.setFormValue(null)
}

2 answers

0 votes
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
July 14, 2022

Hi @Priyanka Nimbalkar,

I have a doubt to clarify, i.e., from the code that you have shared, what type of Behaviour configuration are you using for this? 

Is it a Server-Side Behaviour, or an Initialiser?

If it is the former, then the Behaviour configuration that you have provided will not work because the Server-Side Behaviour will require the field that it is configured for to be declared as:-

def field = getFieldById(fieldChanged)

and you will need to modify your code to something like this:-

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

@BaseScript FieldBehaviours behaviours
def issueManager = ComponentAccessor.issueManager
def customFieldManager = ComponentAccessor.customFieldManager
def epicLink = getFieldById(fieldChanged)
def epicLinkValue = epicLink.value
def sampleTextField = getFieldByName('Sample Text Field')

if (epicLinkValue) {
def issue = issueManager.getIssueObject(epicLinkValue.toString().substring(4))
def epicName = customFieldManager.getCustomFieldObjectsByName('Epic Name').first()
def epicNameValue = issue.getCustomFieldValue(epicName).toString()
sampleTextField.setFormValue(epicNameValue.toString())
}

Please note that the sample code provided is not 100% exact to your environment. Hence, you will need to make the required modifications.

Below is a screenshot of the Behaviour configuration:-

config-1.png

Suppose it is the latter approach you have taken, i.e. using the Initialiser. In that case, it will not work as expected, because updating the custom field depends on the condition based on the value first set to the Epic Link field. 

If you want the behaviour to trigger only after it passes the if / else validation, you will need to use the Server-Side Behaviour.

Below is a test screen for your reference:-

1) When the Create dialog first appears, all the fields are empty

image1.png

2) When I select the Epic Link to Test Epic 1 as expected, the value in the Sample Text Field is updated

image2.png

3) Similar to step 2, when I select Test Epic 2, the Sample Text Field is updated to Test Epic 2 accordingly.image3.png

I hope this helps to answer your question. :)

Thank you and Kind regards,

Ram

 

0 votes
Florian Bonniec
Community Champion
July 13, 2022

Hi

 

I pretty sure the issue is in your if statement.

if(textFieldValue.equalsIgnoreCase("Non-Portfolio")) {

 

You are expecting text content from Epic Link field but it's not the case. It's the epicKey that is store in the field following this format "key:ABC-XX".

 

Can you try 

if(textFieldValue.equalsIgnoreCase("key:<IssueKey>")) {

Where <IssueKey> is the key of your Non-Portfolio Epic.

Suggest an answer

Log in or Sign up to answer