Using Simple Scripted Validator in Jira workflow fails to validate the OR condition

Sreedevi Raman Pisharody June 28, 2023

Hello Team,

I am using the Simple Scripted Validator (Scriptrunner) for the Jira workflow as below :- 

issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_19742")) && isUserMemberOfRole('Product Managers')

||(issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_19742")) == "Testing Only" && isUserMemberOfRole('Test Leads’))

||(issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_19742")) == "Documentation Only" && isUserMemberOfRole('Documentation Authors’))

||(issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_19742")) == "Research" && isUserMemberOfRole('Development Leads’))

||(issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_19742")) == "Engineering Infrastructure" && isUserMemberOfRole('DevOps Leads’))

This script is trying to ensure that if the custom field of type Select list (single choice) satisfies either of the conditions then only validator allows to proceed, but we have noticed that only 1st part is being validated, and if the first condition fails and second one is true it is not processing.

 

I also tried an alternative script for testing as below :- 

cfValues['Feature Type']?.value == 'Testing Only' && isUserMemberOfRole("Test Leads")

|| cfValues['Feature Type']?.value == 'Documentation Only' && isUserMemberOfRole("Documentation Leads")

 

In this case also it is not checking the second condition if first one fails.

 

Please help to identify the cause of why the OR logic is not working for me. Am i missing something.

Thanks and Regards,

Sreedevi

1 answer

1 accepted

0 votes
Answer accepted
Evgenii
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 28, 2023

Hi, @Sreedevi Raman Pisharody 

Your code contains errors. You use wrong quotes, check function isUserMemberOfRole

image.png

Evgenii
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 28, 2023

And you can use code like this (If I correctly understood, what you're trying to do):

/*
* Created 2023.
* @author Evgeniy Isaenkov
* @github https://github.com/Udjin79/SRUtils
*/

String cfValue = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_19742"))

if (cfValue && isUserMemberOfRole('Product Managers')) {
// Do something
}

if (cfValue == "Testing Only" && isUserMemberOfRole('Test Leads')) {
// Do something
} else if (cfValue == "Documentation Only" && isUserMemberOfRole('Documentation Authors')) {
// Do something
} else if (cfValue == "Research" && isUserMemberOfRole('Development Leads')) {
// Do something
} else if (cfValue == "Engineering Infrastructure" && isUserMemberOfRole('DevOps Leads')) {
// Do something
}
Sreedevi Raman Pisharody June 29, 2023

I corrected the quotes it was the error which occurred while copying.

issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_19742")) && isUserMemberOfRole('Product Managers')
||(issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_19742")) == "Testing Only" && isUserMemberOfRole('Test Leads'))
||(issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_19742")) == "Documentation Only" && isUserMemberOfRole('Documentation Authors'))
||(issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_19742")) == "Research" && isUserMemberOfRole('Development Leads'))
||(issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_19742")) == "Engineering Infrastructure" && isUserMemberOfRole('DevOps Leads'))

The syntax shows correct, but in this case it only follows the first case and ignores the OR operator and does not validate the remaining conditions.

Evgenii
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 29, 2023

Hi, @Sreedevi Raman Pisharody 

Have you tried my script above your post?

Sreedevi Raman Pisharody June 29, 2023

Hi Evgeniy,

I was trying the script where you have used if else instead of OR operator and it worked fine. 

Thanks for your help.

But I am unable to understand why is the OR logical operator failing.

Evgenii
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 29, 2023

Understood. Let's split first condition to parts:

issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_19742")) && isUserMemberOfRole('Product Managers')

When you check, that value issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_19742")) exists, it returns false. So, whole condition fails. 

When you check other OR's - the reason is the same, there is no value, in issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_19742")) == "value"... So conditions don't return true

Sreedevi Raman Pisharody June 29, 2023

Oh ok... I think I get you now.

 

Thank you so much for your help

Evgenii
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 29, 2023

Great, @Sreedevi Raman Pisharody !

You're welcome.

If answer helped you, please accept it. It can help other people with similar problems/questions.

Suggest an answer

Log in or Sign up to answer