Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,363,993
Community Members
 
Community Events
168
Community Groups

MyGroovy Workflow Transition Validator Cannot Find Matching Method

Edited

Jira Software 9.0.0 (Server/On-Premise) and MyGroovy 1.22.1-jira8 plugin.

I have a use case where I want to prevent workflow transition using validator if user's non-response to custom field 1) is not captured or default value selected and if user's response is 2) a value other than "No".  Custom field is a radio type custom field with options: "None" (default), "No", and "Yes".

Using Groovy inline script getting two errors:

"Cannot find matching method com.atlassian.jira.issue.mutableissue#getCustomFieldValue(long).  Please check if the declared type is correct and the method exists." on line 9.

"Cannot find matching method java.lang.Object.getCustomFieldValue(java.lang.Object).  Please check if the declared type is correct and the method exists." on line 18.

I've successfully solved for 1) above, so really looking for input on 2) - see script below.

New to this, so any help is appreciated - what am I missing?


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

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def CUSTOM_FIELD_ID = 12345L
def MESSAGE_ERROR = "Field ${getCustomFieldObject(CUSTOM_FIELD_ID)} is required"
def cFieldValue = issue.getCustomFieldValue(CUSTOM_FIELD_ID).getValue()

return cFieldValue

if(!getCustomFieldValue(issue, CUSTOM_FIELD_ID)){
throw new InvalidInputException(MESSAGE_ERROR)
}

def getCustomFieldValue(issue, Long fieldId) {
issue.getCustomFieldValue(getCustomFieldObject(fieldId))
}

def getCustomFieldObject(Long fieldId) {
ComponentAccessor.customFieldManager.getCustomFieldObject(fieldId)
}

if (cFieldValue == "Yes") {
throw new InvalidInputException("Epic cannot transition because Yes response selected - click Cancel")
}

 

2022-09-20_15-41-41.jpg

 

1 answer

0 votes

Hi @Daniel McCollum 

So having the error message when selecting None is normal as None is actually not store as field value. Only No or Yes except if you created a None option manually that is not a good practice.

I would create a behaviour to set this field required in this transition, it will add a * that will guide the user. I assume you are using Scriptrunner.

 

The second error should not prevent the script to work. I think it's just because you did not type your fucntion.

add 

 

import com.atlassian.jira.issue.fields.CustomField

 

Then type your function

CustomField getCustomFieldObject(Long fieldId) {
ComponentAccessor.customFieldManager.getCustomFieldObject(fieldId)
}

Regards

Not using ScriptRunner - strictly Groovy inline script built on the workflow transition via validator. After a quick test/running through transition in question on an active workflow, I've now lost the behavior where user input is required when "None" is selected from the three radio button options.  Perhaps, some reordering of functions in script is needed.

This is the script after adding recommended import and function to script:

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.InvalidInputExceptionimport
// added import recommended
import com.atlassian.jira.issue.fields.CustomFieldCustomField

// added function recommended
getCustomFieldObject(Long fieldId) {
ComponentAccessor.customFieldManager.getCustomFieldObject(fieldId)
}

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def CUSTOM_FIELD_ID = 12345L
def MESSAGE_ERROR = "Field ${getCustomFieldObject(CUSTOM_FIELD_ID)} is required"
def cFieldValue = issue.getCustomFieldValue(CUSTOM_FIELD_ID).getValue()

return cFieldValue

if(!getCustomFieldValue(issue, CUSTOM_FIELD_ID)){
throw new InvalidInputException(MESSAGE_ERROR)
}

def getCustomFieldValue(issue, Long fieldId) {
issue.getCustomFieldValue(getCustomFieldObject(fieldId))
}

def getCustomFieldObject(Long fieldId) {
ComponentAccessor.customFieldManager.getCustomFieldObject(fieldId)
}

if (cFieldValue == "Yes") {
throw new InvalidInputException("Epic cannot transition because Yes response selected - click Cancel")
}

 


This script works flawlessly for requiring selection when "None" is the value:

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.issue.fields.CustomField

def CUSTOM_FIELD_ID = 10405L
def MESSAGE_ERROR = "Field ${getCustomFieldObject(CUSTOM_FIELD_ID)} is required"

if(!getCustomFieldValue(issue, CUSTOM_FIELD_ID)){
throw new InvalidInputException(MESSAGE_ERROR)
}

def getCustomFieldValue(issue, Long fieldId) {
issue.getCustomFieldValue(getCustomFieldObject(fieldId))
}

def getCustomFieldObject(Long fieldId) {
ComponentAccessor.customFieldManager.getCustomFieldObject(fieldId)
}

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events