I want to block transition when my custom field( Customfield_19750) value is "Yes" I want a transition to happen only when a custom field value is "No".
Can you please help me where I am going wrong with my script? Many thanks
Here is the script I am using.
import com.atlassian.jira.component.ComponentAccessor
// Get the custom field manager
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def radioButtonField = customFieldManager.getCustomFieldObject("Field Name")
// Get the value of the radio button field for the current issue
def radioButtonValue = issue.getCustomFieldValue(radioButtonField)
// Check if the radio button value is "Yes"
if (radioButtonValue == "Yes") {
return false // Allow the transition to proceed
}
return true
But I am getting null pointer exception for this one
First off, why are you trying to solve this with a script, there are better options to do this with (though I guess might need a plugin such as JSU or JMWE).
Second, you claim to be getting an NPE but do not say where / show the stacktrace.
Third, the NPE probably comes from
def radioButtonField = customFieldManager.getCustomFieldObject("Field Name"
If you inspect the javadocs, https://docs.atlassian.com/software/jira/docs/api/9.4.18/com/atlassian/jira/issue/CustomFieldManager.html you will see it expects an ID, not a field's name.
getCustomFieldObject(String id)
Get a CustomField by its text key (eg 'customfield_10000').
While you can get a field by name (using a different method), I wouldn't recommend that since you would then practically have to blindly rely on the first element of that list to be your field (without using further filters/checks).
Fourth, you're getting an Object (which is actually a LazyLoadedOption hidden beneath the def) here:
def radioButtonValue = issue.getCustomFieldValue(radioButtonField)
and below it you're doing an equality check on that object against a string:
if (radioButtonValue == "Yes")
Which java-wise should always be false, and afaik groovy should likewise return false due to different data types but who knows really that thing does some weird conversions sometimes.
Either way, you're comparing an Option object with a String, which won't work.
I assume you might be getting NPE because the value is null, which would produce an NPE, trying to perform a method on a null object.
Try
(radioButtonValue != null && radioButtonValue.getValue() == "Yes")
Finally, this seems like a custom validator to me, not a simplified one. Simplified one should have a built-in method to check for a field's value without having to code it from scratch.
The simplified one if I remember correctly works on the principle of true/false result, but a custom one should require throwing a WorkflowException or something similar, it hints that in the function's description somewhere what the expected result of the code should be.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.