Forums

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

Groovy: If component value is selected, a custom field value is required

Jose Sepulveda July 2, 2019

Using Scriptrunner, I'm looking to add a validator on the creation of a ticket that specifies if a certain component field is selected, then another field cannot be null or 'None'.

I was advised with the following:

issue.getComponents()*.name == 'ComponentValueA' && cfValues['Field A']?.value != null

 

If I understand the above statement, it's saying that components is equal to "ComponentValueA" and the custom field "Field A"  is not null.

 

The script is then supposed to check if the above statement is true and only error if it's false. Unfortunately, it seems to always error.

 

I've tried to switch the null to "None" as that's the default value of "Field A" and attempted to treat components as a custom field but bother gave the incorrect results.

2 answers

1 accepted

2 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.
July 2, 2019

It's really not wise to have a literal default value of "None" for select fields. It becomes confusing with the system's apparent default of "None" which means "Null" in db parlance.

If you can search in JQL with:

"Field A" is empty

then you do not have a literal default value of "none". In which case, your original code of != null is appropriate.

Where I see you may get a problem is with issue.component*.name == "ComponentValueA"

That is because the use of * implies that you will return an array.

Aso, if you want to return true when component is something else,

Try this instead: 

!('ComponentValueA' in issue.components*.name  && cfValues['Field A']?.value == null)

This means, return false when the component is ComponentValueA and Field A is Empty.

So if the component is something else, it will return true.
If component is Value A and Field A is not Empty, it will return true

Jose Sepulveda July 3, 2019

Hello Peter,

Understood regarding the null portion. Checked the configuration and found that it was a system value and not one that was created by us as I originally thought. Apologies for the confusion there.

Regarding the query you gave, it worked flawlessly. Exactly what I needed. I appreciate the extra time you took to give a breakdown of what it does as a whole. Makes the next items I have to accomplish that much clearer.

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 3, 2019

Glad I could help.

Don't forget to make the question as answered to help others find this solution.

Krishna February 16, 2020

Hi Peter,

I have problem reading selected component drop down value from components dropdown list. I am using below code to read selected component value and verify the condition, Is there anything wrong with this ?

def issue = ComponentAccessor.getIssueManager().getIssueObject("Job")

if('Technology' in issue.components*.name )
{
//...
}
else
{
//...
}

-Prakash

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.
February 18, 2020

@Krishna , where are you using this code?

"Job" is probably not a valid issue key. You need to have the full key for the current issue. Typically something like "JOB-123"

Look in your script context (the (i) icon below the script editor field. This will show you what variables are already defined for you and issue may already be there.

0 votes
Kian Stack Mumo Systems
Community Champion
July 2, 2019

What type of custom field is "Field A"? A select list?

Jose Sepulveda July 2, 2019

Hello Kian,

Yes, it's a select list single choice.

Suggest an answer

Log in or Sign up to answer