Hi,
I'm trying to add a workflow validator for a sub-task that must look at a field from the parent and only allow the transition if a certain value is selected from a custom single select list field.
In order to accomplish this I was trying to use ScriptRunner's Simple scripted validator by trying to get the value from the sub-task parent (which is a Task issue type) but I can't seem to make it work.
It would be easy for example, to know the status of the parent ticket by doing this:
issue.parentObject.statusObject.name == "Open"
I couldn't, however, find find a way to get the value of a custom field from the parent in order to validate that.
Please help.
Thank you!
To access the parent issue from a simple scripted customfield, you have to use the full api, you can't use cfValues["fieldName"] shortcut.
For example:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.option.Option
def cfm = ComponentAccessor.customFieldManager
def cfId = 10200 //id of the customfield you want to get values from
def cf = cfm.getCustomFieldObject(cfId)
def parentOption = issue.parentObject.getCustomFieldValue(cf) as Option
def parentValue = parentOption.value
Thanks, @Peter-Dave Sheehan !
This is great but how do I actually add the validation to ensure parentValue has the value that is needed inside of the workflow validator?
Should I just add:
parentValue == "XYZ" at the end?
And is there a way to display a message to the user in case the validation fails?
Thanks!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yes, terminate the simple scripted validator with a true/false statement to indicate whether the validation passes or fails.
A true value means the validation passed and no message is displayed.
A false value mean the validation failed and the Error Message you configure in the Simple Scripted Validator form (below the condition script box) will be displayed to the users.
Leave the field parameter empty. This will cause the error message to appear at the top of the form.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello @Peter-Dave Sheehan
I would like to do a similar kind of validation on the parent while creating a subtask. So let me explain what I need.
I want my users to be able to create a subtask only if the value entered in the custom field of the parent is in the right format. I would like to use RegEx within the code to check this. The regex would be something like this : NA|[0-9]{3}. I prefer using the Simple Script validation on the workflow, but I am very new to this Groovy script concept and would like to know how you terminate the script with a true/false value.
Thanks in advance!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I'm assuming you are planning to put a simple scripted validator on the create screen of the sub-task workflow and cause an error when users submit the sub-task if the parent has the wrong value.
In which case something like this might work:
import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.customFieldManager
def customFieldName = 'Name of field to check' //you can also use the id if you prefer
def customField = customFieldManager.getCustomFieldObjectsByName(customFieldName)[0] //this assumes there is only 1 field with this name
def parentValue = issue.parentObject.getCustomFieldValue(customField)
def regExPattern = /NA|[0-9]{3}/
def regExMatcher = parentValue =~ regExPattern
return regExMatcher.matches()
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank you very much @Peter-Dave Sheehan it worked perfectly!
But for an other RegEx pattern, I am getting a wrong result even when the field is blank.
Here is the code:
import com.atlassian.jira.component.ComponentAccessor;
def customFieldManager = ComponentAccessor.customFieldManager
def customFieldName = 'Department'
def customField = customFieldManager.getCustomFieldObjectsByName(customFieldName)[0]
def parentValue = issue.parentObject.getCustomFieldValue(customField)
def regExPattern = /[a-zA-z\/]+[^;]$}/
def regExMatcher = parentValue =~ regExPattern
return regExMatcher.matches()
I want the field to contain only a-z A-Z, slash and not end with semicolon.
This is returning 'True' even if the field is blank (I am not that good at RegEx). Pls correct me if I am wrong.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I'm not great at regex either. Have you tried to validated your pattern in a site like regeex101.com?
You'll need to escape the $ too or change the string delimiter try these variants:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.