Forums

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

Write Behavior for Multi-select Field

Didem Seda Taş March 30, 2022

Hello,

I have a field called Resource Type for Application multiple select list. There are RAM, CPU and Pod Number options in this area. I also have 3 numeric fields named Memory Size, Number of Processors and Pod Count.

My question is;
If I choose the RAM option from my multiple select list field named Resource Type for Application, I want the Memory Size field, if I choose CPU, Number of processors, and if I choose pod, I want the pod count field to appear. Since my Resource type for application field is multiselect, I did not know how to write a behavior.

Can anyone help?

4 answers

2 accepted

3 votes
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.
April 5, 2022

So, you have 3 fields that should be visible or not based on a specific value in a multi-select list being selected or not.

Here is how I would write this:

def multiSelectFieldName = 'Resource Type for Application'
def multiSelectField = getFieldByName(multiSelectFieldName)
def selectedValues = multiSelectField.value as List

def valueFieldMap = [
RAM : 'Memory Size',
CPU : 'Number of Processors',
'POD Number': 'Pod Count'
]

valueFieldMap.each { value, fieldName ->
def showField = selectedValues.contains(value)
getFieldByName(fieldName).setHidden(!showField)
}  
Didem Seda Taş April 6, 2022

Hi Peter,

You always help me. I solved the problem on your page. Thank you very much again.

 

Have a nice day :)

Joel Batac
Contributor
July 10, 2022

@PD Sheehan -- This is wonderful, very elegant code.. Can this be applied to Jira service desk Portal too?

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.
July 10, 2022

Yes, behaviors work pretty much just the same in jira as in service desk portal.

I have found some slight differences when dealing with getting values from Insight custom fields. There are some inconsistencies as to whether you get a string or a size-1 array when a single value is selected. One is a string, the other is an array, I don't remember which is which off the top of my head.

0 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
April 6, 2022

Hi @Didem Seda Taş

You will need to create a Server-Side Behaviour of the Multi-Select List for your requirement. 

For the Behaviour code, you can try something like this:-

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours
def hardwareSelector = getFieldById(fieldChanged)
def hardwareSelectorValue = hardwareSelector.value.toString()

def memorySize = getFieldByName('Memory Size')
def numberOfProcessors = getFieldByName('Number of Processors')
def podCount = getFieldByName('POD Count')

memorySize.hidden = true
numberOfProcessors.hidden = true
podCount.hidden = true

if (hardwareSelectorValue == '[RAM]') {
memorySize.hidden = false
} else if (hardwareSelectorValue == '[CPU]') {
numberOfProcessors.hidden = false
} else if (hardwareSelectorValue == '[POD Number]') {
podCount.hidden = false
} else if (hardwareSelectorValue == '[RAM, CPU]') {
memorySize.hidden = false
numberOfProcessors.hidden = false
} else if (hardwareSelectorValue == '[RAM, POD Number]') {
memorySize.hidden = false
podCount.hidden = false
} else if (hardwareSelectorValue == '[CPU, POD Number]') {
numberOfProcessors.hidden = false
podCount.hidden = false
} else if (hardwareSelectorValue == '[RAM, CPU, POD Number]') {
memorySize.hidden = false
numberOfProcessors.hidden = false
podCount.hidden = false
}

Please note that the sample working code provided is not 100% exact to your environment. Hence, you will need to modify the code accordingly. 

Below is a print screen of the Behaviour configuration:-

configuration.png

Also, I am including a few test print screens:-

1. By default, when no option from the multi-select is picked, all the number fields are hidden, as shown in the image below:-

image1.png

2. When any single option is selected, as shown in the images below, only a single field will be displayed, and the other fields will be hidden.

image2.png

image3.png

image4.png

3. When multiple options are selected, multiple fields will be made visible, as shown in the images below:-

image5.pngimage6.pngimage7.png

4. Finally, if you select all the options, all the fields will be visible as shown below:-

image8.png

I hope this helps to solve your question. :)

Thank you and Kind regards,

Ram

 

Didem Seda Taş April 6, 2022

Thank you very much Ram for your very detailed explanation. It helped a lot.

 

Have a nice day :)

0 votes
Vikrant Yadav
Community Champion
March 30, 2022

Hi @Didem Seda Taş  Behaviour for Multi Select in avaiable on Adaptivist library , you can check this :- 

https://library.adaptavist.com/entity/get-the-values-of-a-multi-select-field

Didem Seda Taş March 30, 2022

Hi Vikrant,

 

I couldn't find the script I was looking for. Thank you for your attention :)

Vikrant Yadav
Community Champion
March 31, 2022

Hi @Didem Seda Taş  You only to do minor change in the given script, it's very simple :- 

You need to apply Behaviour on Multi select list field :- 

import com.onresolve.jira.groovy.user.FieldBehaviours
import org.apache.log4j.Logger
import org.apache.log4j.Level
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours
def log = Logger.getLogger(getClass())

// Set log level
log.setLevel(Level.DEBUG)

def multiSelectField = getFieldByName('MultiSelectA')//multiselect list field name
def ram_size = getFieldByName("Memory Size")// do same for other two fields

// Value for a multi-select field will always be a list even if "None" is selected
def multiSelectFieldValue = multiSelectField.value as List


// If value is RAM
if (multiSelectFieldValue == [RAM]) {
ram_size.setHidden(false)

// If a given string is selected
} else if (multiSelectFieldValue == ["RAM" | "CPU"]) {
ram_size.setHidden(false)
cpu_size.setHidden(false)

// If more than 1 value is selected
} else if ( multiSelectFieldValue.size() > 2 ) {
ram_size.setHidden(false)
cpu_size.setHidden(false)

}

 Try this it should work.

Didem Seda Taş March 31, 2022

Hi again, I made the code you wrote below, but it didn't work, what could be the problem?

 

import com.onresolve.jira.groovy.user.FieldBehaviours
import org.apache.log4j.Logger
import org.apache.log4j.Level
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours
def log = Logger.getLogger(getClass())

// Set log level
log.setLevel(Level.DEBUG)

def multiSelectField = getFieldByName('Resource type for application')//multiselect list field name
def ram_size = getFieldByName("İzin Tipi")// do same for other two fields
def CPU = getFieldByName("İzin Sebebi")// do same for other two fields
def Podsayısı = getFieldByName("HPA Metric")// do same for other two fields

 

// Value for a multi-select field will always be a list even if "None" is selected
def multiSelectFieldValue = multiSelectField.value as List


// If value is RAM
if (multiSelectFieldValue == ["RAM"]) {
ram_size.setHidden(false)
CPU.setHidden(true)
Podsayısı.setHidden(true)
}

// If value is CPU
if (multiSelectFieldValue == ["CPU"]) {
ram_size.setHidden(true)
CPU.setHidden(false)
Podsayısı.setHidden(true)
}


// If value is RAM
if (multiSelectFieldValue == ["Pod Sayısı"]) {
ram_size.setHidden(true)
CPU.setHidden(true)
Podsayısı.setHidden(false)
}

 

// If a given string is selected
else if (multiSelectFieldValue == ["RAM" && "CPU"]) {
ram_size.setHidden(false)
CPU.setHidden(false)
Podsayısı.setHidden(true)

}

// If a given string is selected
else if (multiSelectFieldValue == ["RAM" && "Pod Sayısı"]) {
ram_size.setHidden(false)
CPU.setHidden(true)
Podsayısı.setHidden(false)

}


// If a given string is selected
else if (multiSelectFieldValue == ["Pod Sayısı" && "CPU"]) {
ram_size.setHidden(true)
CPU.setHidden(false)
Podsayısı.setHidden(false)

}

 

// If more than 1 value is selected
else if ( multiSelectFieldValue.size() > 2 ) {
ram_size.setHidden(false)
CPU.setHidden(false)
Podsayısı.setHidden(false)

}
Vikrant Yadav
Community Champion
April 1, 2022

Hi @Didem Seda Taş :- Following script is working when one value is selected in multi select list field or all three selected but not working when selecting two value :(

@Ram Kumar Aravindakshan _Adaptavist_  @PD Sheehan Can you please help us in making the following  script 100% working.

import com.onresolve.jira.groovy.user.FieldBehaviours
import org.apache.log4j.Logger
import org.apache.log4j.Level
import groovy.transform.BaseScript
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours
def log = Logger.getLogger(getClass())

// Set log level
log.setLevel(Level.DEBUG)

def multiSelectField = getFieldById(fieldChanged)//multiselect list field name
def ram_size = getFieldByName("RAM")// do same for other two fields
def CPU = getFieldByName("CPU")// do same for other two fields
def Podcount = getFieldByName("Pod Count")// do same for other two fields


// Value for a multi-select field will always be a list even if "None" is selected
def multiSelectFieldValue = multiSelectField.value as List


// If value is None

if (multiSelectFieldValue == [null]) {
ram_size.setHidden(true)
CPU.setHidden(true)
Podcount.setHidden(true)
}

// If value is RAM
if (multiSelectFieldValue == ["RAM"]) {
ram_size.setHidden(false)
CPU.setHidden(true)
Podcount.setHidden(true)
}


// If value is CPU
if (multiSelectFieldValue == ["CPU"]) {
ram_size.setHidden(true)
CPU.setHidden(false)
Podcount.setHidden(true)
}

// If value is POD NUMBER
else if (multiSelectFieldValue == ["POD NUMBER"] ) {
ram_size.setHidden(true)
CPU.setHidden(true)
Podcount.setHidden(false)

}

// If a given string is selected
else if (multiSelectFieldValue == ["RAM" && "CPU"]) {
ram_size.setHidden(false)
CPU.setHidden(false)
Podcount.setHidden(true)

}

// If a given string is selected
else if (multiSelectFieldValue == ["POD NUMBER" && "CPU"]) {
ram_size.setHidden(true)
CPU.setHidden(false)
Podcount.setHidden(false)

}

// If more than 1 value is selected
else if ( multiSelectFieldValue.size() > 2 ) {
ram_size.setHidden(false)
CPU.setHidden(false)
Podcount.setHidden(false)

}

 

Thanks

V.Y

Didem Seda Taş April 1, 2022

You are very interested @Vikrant Yadav . Thanks a lot for your support :)

Like Vikrant Yadav likes this
Didem Seda Taş April 5, 2022

@PD Sheehan 

 

@Ram Kumar Aravindakshan _Adaptavist_ 

 

could you please help me?

0 votes
Jack Brickey
Community Champion
March 30, 2022

It seems what you want can be provided by the Cascading select field. There are two levels for this field where the options in level 2 are conditioned by the choice made in level 1. Can you give that a try?

Didem Seda Taş March 30, 2022

Hi Jack, thank you very much for your interest :)

I'm not good at behavior. I don't know how to edit the way you mentioned :(

Jack Brickey
Community Champion
March 30, 2022

I think I may have misread your original post. My suggestion was not associated with Behaviors at all. Rather I was suggesting the use of a cascading select field type.

Suggest an answer

Log in or Sign up to answer