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

ScriptRunner behavior to set fields based on another field

Michael Wolfe March 31, 2022

Full disclosure, I am new to ScriptRunner and Groovy scripting, however I have scripting experience in other languages.

As the title says, I want to set the value of Field B based on what is selected in Field A.  We're doing this in another behaviour, so I have stolen the code and changed the relevant field names to match the ones I need.  I'm trying to determine if the result is a bug, or if I'm missing something.

What happens is upon selecting an option in Field A, Field B changes from "None" to just nothing.  If you click the drop down menu, the desired option is the only one available.  Here is my code:

// Import classes needed
import com.atlassian.jira.component.ComponentAccessor
def optionsManager = ComponentAccessor.getOptionsManager()

// Get CFs and create options manager
def subCategory = getFieldByName("Sub-Category Local").getValue() as String
def categoryRF = getFieldByName("Category RF")
def customField = customFieldManager.getCustomFieldObject(categoryRF.getFieldId())
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)

// Checks Sub-Category and sets Category RF options
if(subCategory == "Conversion costs (cups)")
{
categoryRF.setFieldOptions(options.findAll {it.value in ['Plastics']})
}

 

Here are screen shots of what is displayed after making the selection.

Screen Shot 2022-03-31 at 4.13.14 PM.png


Screen Shot 2022-03-31 at 4.13.32 PM.png

 

Like I said, this code was taken from another behaviour we have that does the same thing for another project, I've just changed the fields. 

1 answer

2 votes
Fernando Eugênio da Silva
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 31, 2022

@Michael Wolfe 

Have you tried adding an ELSE negating the entire IF condition?

So the behavior would be: if the IF is not true, assume normal behavior.

else {
subCategory.setRequired(false)
subCategory.setHidden(false)
subCategory.setReadOnly(false)

categoryRF.setRequired(false)
categoryRF.setHidden(false)
categoryRF.setReadOnly(false)
}

Or you can just add a new IF by saying:

if ("subCategory != "Conversion costs (cups)") {

subCategory.setRequired(false)
subCategory.setHidden(false)
subCategory.setReadOnly(false)

categoryRF.setRequired(false)
categoryRF.setHidden(false)
categoryRF.setReadOnly(false)
}

Wouldn't something like that work?

Michael Wolfe April 1, 2022

@Fernando Eugênio da Silva 

I haven't tried that yet, however, this is going to be a giant "else if" clause as there are 31 subCategory statements in total that need to be set.  I wanted to make sure one would work before I wrote out all of them.  This also doesn't include another custom field that has cascading fields that will also change depending on the subCategory, but I'll deal with one thing at a time.

Fernando Eugênio da Silva
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 1, 2022

@Michael Wolfe 

If you don't want to assign behavior to each field option, you don't need to include such a large load.

I believe that following the example I gave you will get a good result because the second IF or ELSE will make the field behavior in Jira be the default with all other options in the other fields.

But I agree with you when you say go one thing at a time.

I wish you a good job. You can!

Michael Wolfe April 1, 2022

@Fernando Eugênio da Silva 

Just for the sake of trying something different, I added a few more options to see if there was an issue with the parenthesis.  What I have found out is that choosing any of the options will first fail, and then subsequent selections work to include the one that initially didn't work.  Any idea what could be causing this?

Fernando Eugênio da Silva
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 1, 2022

@Michael Wolfe 

I don't know exactly why this happens. Something like what you describe has never occurred to me.

It could be something with the order they are arranged, try changing the order and see if the behavior is still the same.

Michael Wolfe April 1, 2022

@Fernando Eugênio da Silva 

I've been playing with an ending "else" that sets the Category RF field to "None", and that seems to be "fixing" the problem.  However, the Category RF looks like this when the Create form loads.  That's better, but not idea.

 

Screen Shot 2022-04-01 at 8.13.43 AM.png

Fernando Eugênio da Silva
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 1, 2022

@Michael Wolfe 

Try changing "none" to "null".

This is because interpretations can change.

"null" = EMPTY
"none" = no value

 

If that doesn't work then you can go on trying some combinations in this ELSE. Something like

RF != Plastics

categoryRF.setRequired(false)
categoryRF.setHidden(false)
categoryRF.setReadOnly(false)

I think it's important to always emphasize what the behavior will be through these actions here:
"categoryRF.setRequired(false)
categoryRF.setHidden(false)
categoryRF.setReadOnly(false)"

Allows me to better manipulate the behavior, it might not be your case, but it's something to consider.

Michael Wolfe April 1, 2022

@Fernando Eugênio da Silva 

So I have added the following at the end of my script, and it allows the Category RF to show the default "None" option. 

else 
{
categoryRF.setFormValue(null)
categoryRF.setRequired(false)
categoryRF.setHidden(false)
categoryRF.setReadOnly(false)
}

However, it hasn't resolved the issue where the first selection of the Sub-Category field changes the Category RF field to a blank option.  Strangely enough, it's only the Category RF field.  I have added code to manipulate the Cascading fields, and it changes those without any issues.

Fernando Eugênio da Silva
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 1, 2022

In that case, I believe you should set the behavior of the CategoryRF field, using the Sub-Category field

Like:

IF ELSE { ("subCategory !="Conversion costs (cups)")

subCategory.setRequired(false)
subCategory.setHidden(false)
subCategory.setReadOnly(false)

categoryRF.setRequired(false)
categoryRF.setHidden(false)
categoryRF.setReadOnly(false)
}

Michael Wolfe April 1, 2022

@Fernando Eugênio da Silva 

So it turns out it could have been partly the version I had on my server, 6.26, and partly been how I was setting it.  Here's what I should have been doing:

 

if(subCategory == "Conversion costs (cups)")

{
categoryRF.setFormValue('Plastics')
}

Thanks for trying to help me solve this weird behavior.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events