Forums

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

Scriptrunner - How to show / hide field when another field changes?

Rune Hellem May 20, 2020

 

Version

Jira v8.4.2, Scriptrunner 6.0.2-p5 

Scenario

When a custom field drop down menu changes I need to trigger a script that show/hide another drop down menu based on the value of the first one. The code needed to do this can be seen in my question from yesterday

Problem

I have not understood how I can trigger the script when the parent field changes. I have not been able to see that I can capture this in my "Behaviour". If I am not missing anything out by what is shown in this video made by Adaptavist the server side script should be triggered simply by just changing the value of the drop down menu, no need to select "Create" or "Update". The video shows help text showing at once and also tabs being hidden/visible on the fly.

Do I need to configure Scriptrunner in any way to support server side script being triggered just by field update?

 

2 answers

1 accepted

1 vote
Answer accepted
PD Sheehan
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.
May 23, 2020

Let's see if I can walk you through the steps.

First, make sure you have a behaviour configuration with the correct project/issue type mapping.

Then go into the field section of the behavior.

Select the "parent' field from the drop-down at the bottom in the add field selection.

Then in the section for that parent field, add a server-side script.

The server-side script will execute when the form is loaded and every time the value is changed in that field.

The script can be much simpler than what you linked to (which seems to be more an event listener script than a behavior script).

Here is a sample script that you can adapt:

def optionsForVisible = ['option1']
def fieldToShowOrHide = 'name of field to show/hide'
def triggerField = getFieldById(getFieldChanged())
def triggerValue = triggerField.value
def targetField = getFieldByName(fieldToShowOrHide )
targetField.setHidden(!(triggerValue in optionsForVisible)).setRequired(!targetField.isHidden())
Rune Hellem May 29, 2020

Thanks!

I have in the meantime learned that what I wanted in the first place was not a drop down menu (Single Select) but a Cascading Drop Down selection. But I still need to make the second drop down mandatory if values, so the script is needed.

0 votes
Shamanth
Contributor
February 19, 2023

@PD Sheehan , can I hide a custom field in the subtask based on a single select value of a field in the parent task? I am trying to do this using behaviors, but I am unable to figure out how to get the parent value on the code.

PD Sheehan
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 21, 2023

Yes, there is a hidden field available to the behaviour script when you create a sub-task.

You can find that by starting a sub-task creation screen and examining the network traffic from your browser's developer panel.

There you will see a field sent to scriptrunner called parentIssueId.

From that, you get derive an issue object and fetch the value from the custom field.

For example:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.option.Option
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

def parentIssueFld = getFieldById('parentIssueId')
if(!parentIssueFld) return //this is a not a sub task creation

def parentIssueId= parentIssueFld.value as Long
def parentIssue = ComponentAccessor.issueManager.getIssueObject(parentIssueId)

def selectCf = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName('Name of select field')[0]
def parentSelectValue = parentIssue.getCustomFieldValue(selectCf) as Option

def shouldBeHidden = parentSelectValue.value == 'Value that hides another field'
def otherField = getFieldByName('Other field name')
otherField.hidden = shouldBeHidden
Like Shamanth likes this
Shamanth
Contributor
February 22, 2023

@PD Sheehan Its works perfectly! Thank you very much!

Suggest an answer

Log in or Sign up to answer