Forums

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

scriptrunner validation to check summary starts with a custom field

David Mallon October 31, 2018

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

 

1 answer

1 accepted

1 vote
Answer accepted
Ivan Tovbin
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.
November 1, 2018

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.

David Mallon November 1, 2018

Hi Ivan, thanks so much. I am getting this error, I only changed field name to Incident Number once as shownstatic.png

Ivan Tovbin
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.
November 2, 2018

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 :))

David Mallon November 4, 2018

Hi Ivan, its a text field with Letters and numbers. sorry for confusion

David m

Ivan Tovbin
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.
November 5, 2018

No problem. I've updated the code above.

David Mallon November 6, 2018

Thanks Ivan, that is awesome

much appreciated

David m

Suggest an answer

Log in or Sign up to answer