Forums

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

Scriptrunner - JIRA - Show child field based on parent field value

Rune Hellem May 19, 2020

Version

Jira v8.4.2, Scriptrunner 6.0.2-p5 

Scenario

Drop down menu with four options, if value "Standard change" is selected, another drop down menu should be shown, being mandatory as well. Drop down menu should be shown immediately (client side scripting).

What I have tried

Totally newbie to Scriptrunner. I have tried to implement this using a Behaviour. Reading Scriptrunner doc, Behaviours Java-Script based it says "...Behaviours rely on client-side javascript.....However, the typical use cases are to prompt the user that they should be entering another field depending on some condition, or to hide a field for clarity’s sake. In these cases, the user is better off with behaviours, and no worse off than they were without them."

I did first try to implment the below script as "Server side script", but then guessing "Initialiser" is what I needed. But now also realizing that maybe Initalizer is "to early" and "server side" is to late?

The script (compiled from various sources using Google)

import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.fields.Field
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import org.apache.log4j.Level


log.setLevel(Level.DEBUG)
final String parentDropDownFieldName = "CM Change type category"
final String childDropDownFieldName = "CM Standard Change Types"

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def parentDropDownFieldNameFields = customFieldManager.getCustomFieldObjectsByName(parentDropDownFieldName)
def CustomField parentDropDownFieldNameCustomField = parentDropDownFieldNameFields.iterator().next()
def childDropDownFieldNameFields = customFieldManager.getCustomFieldObjectsByName(childDropDownFieldName)
def CustomField childDropDownCustomFieldNameField = childDropDownFieldNameFields.iterator().next()

log.debug("Show parentDropDownFieldNameField" + parentDropDownFieldNameCustomField)

def selectedOption = underlyingIssue.getCustomFieldValue(parentDropDownFieldNameCustomField).toString()
def isStandardSelected = selectedOption == "Standard Change"
def childDropDownMenuField = getFieldById(childDropDownCustomFieldNameField.id)

log.debug("The fieldName='" + parentDropDownFieldName + "' with fieldId='" + parentDropDownFieldNameCustomField.id + "' has value=" + selectedOption + " and therefore isStandardSelected=" + isStandardSelected)

Issue issue = event.issue
log.debug("Value of event=" + event)
log.debug("Value of issue=" + issue)
def change = event?.getChangeLog()?.getRelated("ChildChangeItem").find {it.name == parentDropDownFieldName}
log.debug("Value of change=" + change)
if (change && isStandardSelected) {
def changeHolder = new DefaultIssueChangeHolder()
log.debug("Make field '" + childDropDownFieldName + "' visible")
childDropDownMenuField.setHidden(false)
childDropDownMenuField.setRequired(isStandardSelected)
} else {
log.debug("Make field '" + childDropDownFieldName + "' hidden")
childDropDownMenuField.setHidden(true)
childDropDownMenuField.setFormValue(null)
childDropDownMenuField.setRequired(isStandardSelected)
}

I am a bit confused about the event.issue-part, since I get the error

groovy.lang.MissingPropertyException: No such property: event for class:

Realizing that I am a bit lost

1 answer

1 accepted

0 votes
Answer accepted
Rune Hellem May 21, 2020

Realized a few things - the script does what I expect, that is hide or show the field based on value of parent field. Also I have figured out that placing it in the "Server side script"-part is the correct place. at least as long as I have a debug line in the initaliser it will trigger the serverside script on load and also when issue is saved/updated.

BUT - I have yet to figure out how the server side script can be triggered when the drop down menu value is changed, so the hide/enable is done on the fly. It should be done just out of the box as shown in this video https://www.youtube.com/watch?v=lHJivD0MfFg where the help text is shown immediately, and later the tabs swhich on the fly.

I have therefore asked new question where I only asks one question, not two as I did here (both show/hide and "on the fly")

Suggest an answer

Log in or Sign up to answer