Show specific values based on previous custom field

Raynard Rhodes
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.
October 25, 2019

I'm looking for a way to display only certain values of a customfield based on the value of a previous customfield. I do not want to hide/show the entire customfield, only limit the values available. Is there a way to achieve this?

 

import com.atlassian.jira.component.ComponentAccessor

def optionsManager = ComponentAccessor.getOptionsManager()
def cfManager = ComponentAccessor.getCustomFieldManager()

def lobField = getFieldById("LOB:")
def lobProgram = getFieldById("LOB:Programs")
def lobProject = getFieldById("LOB:Project")

def cfLobField = cfManager.getCustomFieldObjectByName("LOB:Programs")
def fcLobField = cfLobField.getRelevantConfig(issueContext)
def lobOptions = optionsManager.getOptions(fcLobField)

def analyzeSet = ["MGV", "BLAST"]


if(lobField.getValue() == "Analyze"){
lobProgram.setFieldOptions(lobOptions.findAll{it.value in analyzeSet}.collectEntries { [(it.optionId).toString() in it.value] })
}

1 answer

1 accepted

2 votes
Answer accepted
Kevin Johnson
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 29, 2019

Hi dude 

Ill try out in my instance and share you the code .

Kevin Johnson
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 30, 2019

Hi @Raynard Rhodes 

I have created a behaviour and two custom fields - data base type and data base version 

and in the behaviour select the first field based on which the second custom field value should be filtered.This code works out for me try this in your instance and later modify it accordingly.

Cheers ...!

 

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def optionsManager = ComponentAccessor.getOptionsManager()


def picklist8 = getFieldByName("Database Type")

def picklist9 = getFieldByName("Database Versions")

def customField =
customFieldManager.getCustomFieldObject(picklist8.getFieldId()) // Pick list Field that dictates other fields on form based on its value

def config = customField.getRelevantConfig(getIssueContext())

def options = optionsManager.getOptions(config)




def customField1 =
customFieldManager.getCustomFieldObject(picklist9.getFieldId()) // Pick list Field that dictates other fields on form based on its value

def config1 = customField1.getRelevantConfig(getIssueContext())

def options1 = optionsManager.getOptions(config1)




if (picklist8.getValue() as String == "My SQL") {




def optionsMap8 = options1.findAll

{it.value in ["MySQL 5.5","MySQL 5.7"] // list of options you want to show on Biz Priority Field

}.collectEntries {

[

(it.optionId.toString()) : it.value

]

}

picklist9.setFieldOptions(optionsMap8)

}




else if (picklist8.getValue() as String == "MS SQL"){




def optionsMap8 = options1.findAll{

it.value in ["MSSQL 2014","MSSQL 2016"] // list of options you want to show on Biz Priority Field

}.collectEntries {

[

(it.optionId.toString()) : it.value

]

}

picklist9.setFieldOptions(optionsMap8)


}

else if (picklist8.getValue() as String == "SQL Lite") {

def optionsMap8 = options1.findAll

{

it.value in ["SQL Lite 2.1","SQL Lite 3.0"] // list of options you want to show on Biz Priority Field

}.collectEntries {

[

(it.optionId.toString()) : it.value

]

}

picklist9.setFieldOptions(optionsMap8)

}
Like Raynard Rhodes likes this
Raynard Rhodes October 30, 2019

@Kevin Johnson I appreciate your assistance. A bit later that day I did get my code working. I'll post my results Monday. I'll also give your code a try and may be effectively code mine. 

Like Kevin Johnson likes this
Raynard Rhodes
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.
November 4, 2019

Below is the code I used.

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

@BaseScript FieldBehaviours fieldBehaviours

Issue issue = underlyingIssue as Issue
def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()

FormField lob = getFieldByName("LOB: ")
FormField lobPrograms = getFieldByName("LOB:Programs ")
FormField lobProject = getFieldByName("LOB:Project ")


def lobPrograms2 = customFieldManager.getCustomFieldObjectByName("LOB:Programs ")
def lobProject2 = customFieldManager.getCustomFieldObjectByName("LOB:Project ")

def lobProgramsValue = lobPrograms.getValue()
def lobValue = lob.getValue()

def fieldConfig = lobPrograms2.getRelevantConfig(issue)
def fieldConfig2 = lobProject2.getRelevantConfig(getIssueContext())

def allowedOptions = null

def getOptions = optionsManager.getOptions(fieldConfig)
def getOptions2 = optionsManager.getOptions(fieldConfig2)

if(lobValue=="Option1 || lobValue=="Option2){
lobPrograms.setFieldOptions(getOptions.findAll{
it.value in ["This is to be shown", "This one, too!"]
})}

if(lobValue=="Option1" && lobProgramsValue=="This is to be shown"){
lobProject.setFieldOptions(getOptions.findAll{
it.value in ["This is the third option"]
})}

The first if statement is to change field 2 based on field 1. The second if statement is to change field 3 based on field 1 and 2. I just keep repeating the if statement for each unique change. 

Like Kevin Johnson likes this
Kevin Johnson
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 5, 2019

@Raynard Rhodes great ..!

Suggest an answer

Log in or Sign up to answer