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.
Can you kelp me?
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.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Please try below method and let me know if it works. It's versatile.
Thanks,
Pramodh
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.