Groovy Script Post function validator no longer working after migration to 7.1.10

We recently upgraded from jira 6.3.1 to 7.1.10, incremental updates as specified. we have a Validator script on the create that makes Multi-Level Cascading Select drop-down fields mandatory, this is no longer mandatory

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.ManagerFactory
import com.atlassian.jira.issue.security.IssueSecurityLevelManager
import com.atlassian.jira.issue.security.IssueSecuritySchemeManager
import org.apache.log4j.Category
import org.apache.log4j.Logger
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import org.ofbiz.core.entity.GenericValue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.Issue
import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.issue.fields.CustomFieldImpl

def log = Logger.getInstance("com.onresolve.jira.groovy.Validator")

log.setLevel(org.apache.log4j.Level.DEBUG)
log.debug("********************************** Service Group Selector Validator ***********************************************")

//ComponentManager componentManager = ComponentManager.getInstance()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjectByName("Service Group Selector")

log.debug("Obtained Service Group Selector custom field")
def myIssue = issue
Map cfVal = myIssue.getCustomFieldValue(cf) as Map

log.debug("Converted field to map")

String first = cfVal.get(0)
String second = cfVal.get(1)
String third = cfVal.get(2)

log.debug("Service Group Validator: " + first)
log.debug("Service Name validator: " + second)
log.debug("Service Category validator: " + third)

def matchString = 'com.sourcesense.jira.plugin.customfield.option.SpecialOptionFactory$SpecialOption@0'

if ((first == matchString) || (third == matchString) || (second == matchString))
{
log.debug("Invalid options selected for service category")
log.debug("raising exception")
InvalidInputException invalidInputException = new InvalidInputException("Service Group Selector")
invalidInputException.addError("Service Group Selector", "You must fill out all the Service Group Selector field values")
throw invalidInputException
}

log.debug("Valid options selected for service category")

 

Error:

it fails after the 'Converted field to map'

Script function failed on issue: null, actionId: 1, file: serviceGroupValidator.groovy

1 answer

I assume that by "this is no longer mandatory", you mean that the validator just isn't validating the way you'd expect.

This may well be an API change in the Multi-Level Cascading Select plugin. I noticed from their version history that there's a compatibility break between 3.2 (which works with JIRA 6) and 4.0 (which works with JIRA 7).

They've even got some ScriptRunner examples in their docs, with different code for JIRA 6 and 7. 

Assuming that you just want to check that they've filled in every level, I think you could do this:

import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.InvalidInputException

log.debug("********************************** Service Group Selector Validator ***********************************************")
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjectByName("Service Group Selector")
log.debug("Obtained Service Group Selector custom field")
def myIssue = issue
def cfVal = myIssue.getCustomFieldValue(cf)
boolean passes = cfVal.any{
    it.toString() == 'com.sourcesense.jira.plugin.customfield.option.SpecialOptionFactory$SpecialOption@0'
}

if (!passes) {
    log.debug("Invalid options selected for service category")
    log.debug("raising exception")
    InvalidInputException invalidInputException = new InvalidInputException("Service Group Selector")
    invalidInputException.addError("Service Group Selector", "You must fill out all the Service Group Selector field values")
    throw invalidInputException
}
log.debug("Valid options selected for service category")

Or, following their example:

import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.InvalidInputException

log.debug("********************************** Service Group Selector Validator ***********************************************")
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjectByName("Service Group Selector")
log.debug("Obtained Service Group Selector custom field")
def myIssue = issue
def cfVal = myIssue.getCustomFieldValue(cf)
boolean passes = cfVal.any{
    it.toString().startsWith('com.sourcesense.jira.plugin.customfield.option.SpecialOptionFactory')
}

if (!passes) {
    log.debug("Invalid options selected for service category")
    log.debug("raising exception")
    InvalidInputException invalidInputException = new InvalidInputException("Service Group Selector")
    invalidInputException.addError("Service Group Selector", "You must fill out all the Service Group Selector field values")
    throw invalidInputException
}
log.debug("Valid options selected for service category")

 

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

161 views 0 5
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you