Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Scriptrunner behaviour: Contains function

Hello,

I'm trying to filter the value of Field2 (Select List (multiple choice) depending on the value of Field1 (Select List (multiple choice).

 

However, it only reads the ((selectedOption.contains("Client"))) condition and I believed this is because they both contain a 'Client' keyword.

 

Is there a way to be more specific? I tried using (selectedOption == "Client Team") but its not working.

 

Here is my code below.

 

import com.atlassian.jira.component.ComponentAccessor

def tool = getFieldByName("Plan Type")

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

def toolfield = getFieldById(getFieldChanged())
def selectedOption = toolfield.getValue() as String

def customField = customFieldManager.getCustomFieldObject(tool.getFieldId())
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)

tool.setRequired(false)
tool.setHidden(true)
tool.setFormValue(null)

if ((selectedOption.contains("Client Team")))
{
def optionsMap2 = options.findAll {
it.value in ["CT: Medical", "CT: Vision", "CT: Dental", "CT: Pharma (Rx)"]
}.collectEntries {
[
(it.optionId.toString()): it.value
]
}

tool.setFieldOptions(optionsMap2)
tool.setRequired(true)
tool.setHidden(false)
}


if ((selectedOption.contains("Client")))
{
def optionsMap3 = options.findAll {
it.value in ["Client: Medical", "Client: Vision", "Client: Dental", "Client: Pharma (Rx)"]
}.collectEntries {
[
(it.optionId.toString()): it.value
]
}

tool.setFieldOptions(optionsMap3)
tool.setRequired(true)
tool.setHidden(false)
}

 

2 answers

if ((selectedOption.contains("Client Team"))) {
//logic for client team
} else if ((selectedOption.contains("Client"))) {
//logic for client
}

otherwise it enters both of your cycles if it contains "Client Team" and 2nd cycle being last overrides logic of first 

Hi @Ysaac James Salao 

You retrieve the values of your fields as a String. And contains operator returns true if the string contains the specified sequence of char values

But multi chose select list return values as a List<String>. And contains operator returns true if the list contains the specified element.

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Marketplace Apps & Integrations

Happy New Year and Welcome New Marketplace Partner Program Partners!

Happy New Year! We hope you all had a safe and restful holiday season. 2020 was a unique year full of unforeseen events; however, as we enter the new year of 2021, we’re optimistic for the light at t...

463 views 6 18
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you