Forums

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

Custom scriptrunner validator with select field and number

valeria cruz
Contributor
September 10, 2021

Hello guys!

I"m trying to do a custom script validation using script runner.

My script needs to validate two types of scenarios.

1 - custom field ST( type select list) with values A or B and custom field CP (type number field) with no value (null) needs to return an error because the CP field can't be empty if ST has value A or B.


2- The second scenario is similar, but the field CP can be empty if the ST field has C or D values.


Follow my script

 

import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.atlassian.jira.issue.managers.DefaultIssueManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.user.ApplicationUser

 

def issueManager = ComponentAccessor.getIssueManager()
def userManager = ComponentAccessor.getUserManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def ST = customFieldManager.getCustomFieldObjectsByName("Story Type")
def CP = customFieldManager.getCustomFieldObjectsByName("Complexity Points")


if (ST == 'C' || ST == 'D' && CP == "null") {
return true
log.info("ST C AND D")
}

else {
(ST == 'A' || ST == 'B' && CP == "null")
return false
throw new InvalidInputException ("CP can be empty")
log.info("ST A AND B")
}

 


It seems that the validation script isn't running (but is running), and I didn't receive the log info return.
In attachments are some prints from my WF.

1.JPG2.JPG

Can you kelp me?

4 answers

0 votes
Prince Nyeche
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.
September 12, 2021

Why is null a string within your code block? It seems to me the script ran and found that the condition set didn't match, so it skipped the block and exited.

0 votes
rahuldanwade
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.
September 11, 2021

Hello Valeria,

ST & CP are references to the Custom field objects.

You need to use the below methods to fetch the cf values for the issue. 

ST.getValue(issue)

CP.getValue(issue)

You can use a simple one-line validator from Scriptrunner rather than writing the script.

May explore cfValues built-in wrapper/map-like structure to fetch CF values rather than importing many classes and writing script for simple validations.

 

Thanks

Rahul

0 votes
Nic Brough -Adaptavist-
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.
September 10, 2021

The green "script has run ok" flag is only telling us that the script ran and "worked" - for a validator, that means it returned something to Jira that was a "yes or no on the validation question".  It will only go red/fail when the script fails or crashes and returns "it's all gone horribly wrong"

For the rest of the question about comparing strings to option objects, I wrote something on this a few hours ago, over at https://community.atlassian.com/t5/Jira-questions/Custom-Script-Post-Function-ScriptRunner/qaq-p/1801628

0 votes
Pramodh M
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 10, 2021

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.13.3
TAGS
AUG Leaders

Atlassian Community Events