Hi , I am trying to create a workflow transition validation
I need to check that the issue summary starts with a custom field called Incident Number if the Incident Number field is populated
I can get it to work if the summary exactly matches the Incident Number Field using,
!cfValues['Incident Number'] || issue.summary == (cfValues['Incident Number'])
however I need the summary to start with the Incident Number field and then contain free text
Thanks
David m
Hi David,
To get this to work, you need to match your summary against a regex, containing your custom field value. Try this:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
import com.opensymphony.workflow.InvalidInputException
import java.util.regex.Matcher
import java.util.regex.Pattern
CustomField customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("fieldName")
String cfValue = issue.getCustomFieldValue(customField)
if(cfValue){
String regex = "^${cfValue)}"
String summary = issue.getSummary()
if(!getMatchedString(regex, summary, 0)){
throw new InvalidInputException("Summary must begin with ${customField.getFieldName()} value!")
}
return true
}
return true
String getMatchedString(String regex, String stringToMatch, int matcherGroupIndex){
String result = ""
if(regex && stringToMatch){
Matcher matcher = Pattern.compile(regex).matcher(stringToMatch)
if(matcher.find() && matcherGroupIndex <= matcher.groupCount()){
result = matcher.group(matcherGroupIndex)
}else{
log.error("No match found!")
}
}
return result
}
Just in case your number field has no value at all, the validation will also pass. Feel free to change it if you need it.
Hope this helps.
Hi Ivan, thanks so much. I am getting this error, I only changed field name to Incident Number once as shown
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Feel free to ignore this error. Static type checking in Scriptrunner is not always accurate. It should work as is (assuming your custom field type is numbers field :))
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Ivan, its a text field with Letters and numbers. sorry for confusion
David m
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
No problem. I've updated the code above.
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.
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.