Forums

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

ScriptRunner - How To Conditionally Display A Set of Values

Ricky Wang Lin
Contributor
September 14, 2021

Setup:

Two custom fields:

  • CF 1 = Single Select Field with options of High and Low
  • CF 2 = Single Select Field with options of 1-10

Scenario:

  • IF CF 1 = High, then CF 2 = 1-5
  • If CF 2 = Low, then CF 2 = 6-10

How can you accomplish this with ScriptRunner during a transition screen?

1 answer

1 accepted

1 vote
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
September 14, 2021

Hi @Ricky Wang Lin

A similar question was asked and answered with a sample working code in this Community Post.

The only difference between the example code provided in the Community Post and your requirement is the values used in the Single Select List field.

I hope this helps to solve your question. :)

Thank you and Kind Regards,

Ram

Ricky Wang Lin
Contributor
September 15, 2021

Hi @Ram Kumar Aravindakshan _Adaptavist_  - thank you very much for sharing this.  I went through your sample code and modified it but it didn't seem to work for me...

In my case, list1 = Event Classification and list 2 = Event Category

Below is my code modified used:

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

@BaseScript FieldBehaviours fieldBehaviours

def list1 = getFieldById(fieldChanged)
def list1Value = list1.value.toString()

def list2 = getFieldByName("Event Category")

def optionManager = ComponentAccessor.optionsManager
def customFieldManager = ComponentAccessor.customFieldManager

def list2CustomField = customFieldManager.getCustomFieldObject(list2.fieldId)
def list2Config = list2CustomField.getRelevantConfig(issueContext)
def list2Options = optionManager.getOptions(list2Config)

def listBNewOptions

if(list1Value == "Incident") {
listBNewOptions = ["1 - Root Level Intrusion (Incident)", "2 - User Level Intrusion (Incident)", "4 - Denial of Service (Incident)", "7 - Malicious Logic (Incident)"]
} else if(list1Value == "Event") {
listBNewOptions = ["3 - Unsuccesful Activity Attempt (Event)", "5 - Non-Compliance Activity (Event)", "6 - Reconnaissance (Event)", "8 - Investigating (Event)", "9 - Explained Anomaly (Event)"]
} else {
listBNewOptions = ["1 - Root Level Intrusion (Incident)", "2 - User Level Intrusion (Incident)", "4 - Denial of Service (Incident)", "7 - Malicious Logic (Incident)", "3 - Unsuccesful Activity Attempt (Event)", "5 - Non-Compliance Activity (Event)", "6 - Reconnaissance (Event)", "8 - Investigating (Event)", "9 - Explained Anomaly (Event)"]
}

def listBNewOptionsMap = [null: "None"]

listBNewOptionsMap += list2Options?.findAll {

it.value in listBNewOptions
}?.collectEntries {
[ (it.optionId.toString()) : it.value ]
}

list2.setFieldOptions(listBNewOptionsMap)


I am curious about this following line in your code as you pointed out in your example:

def list1 = getFieldById(fieldChanged)
def list1Value = list1.value.toString()

How does this tell ScriptRunner that list1 = Event Classification?

When I tried to replace it with the following, I started getting error:

def list1 = getFieldByname("Event Classification")
def list1Value = list1.value.toString()

Would you be able to provide more insight?  Thank you

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
September 15, 2021

Hi @Ricky Wang Lin

The fieldChanged option is used to ensures that the Behaviour is triggered when any change is made to the Field the Server-Side Behaviour is configured for.

If you observe the print screen in the  Community Post, as shown below, you will notice the Server-Side Behaviour has been configured for the field List 1.

behaviour_config.png

That is why, in the code, to invoke the List 1 fieldChanged is used, i.e.:-

def list1 = getFieldById(fieldChanged)

Could you provide a print screen of your Behaviour configuration along with a print screen of the create or edit screen where these fields are used?

Thank you and Kind Regards,

Ram

Like Ricky Wang Lin likes this
Ricky Wang Lin
Contributor
September 15, 2021

@Ram Kumar Aravindakshan _Adaptavist_- thank you so much for pointing that out - sorry I'm still very much a newbie with ScriptRunner.

I put my code as an initializer instead of a server-side script.  I deleted the initializer, created a server-side script while picking Event Classification as the field and it worked like a charm... THank you so much!


If you don't mind, I have another open question that I can use some hint on if you could so kind so shed some light (https://community.atlassian.com/t5/Jira-questions/ScriptRunner-How-To-Restrict-Issue-Type-Creation-By-Groups/qaq-p/1804874#M499634).   Thank you Ram!!

Suggest an answer

Log in or Sign up to answer