Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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

 

2 answers

2 accepted

0 votes
Answer accepted

Reference accepted solution here

0 votes
Answer accepted
Florian Bonniec
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Sep 20, 2022

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

Hi @Florian Bonniec , appreciate your last feedback help with above script!

I was not able to solve where to add function within the script - very new to mygroovy.

Script below works fine for requiring user response when "None" is the value selected - other two aerial options are "Yes" and "No" besides "None":

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

What I would like to accomplish next with script is when user selects "Yes", grey-out/disable the Not Applicable button to prevent transition - is this achievable?

Thanks in advance!

Florian Bonniec
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Oct 11, 2022

Hi @Daniel McCollum 

You cannot grey-out but you can block the transition by adding an error message like you did for the none.

 

if(getCustomFieldValue(issue, CUSTOM_FIELD_ID).name == "Yes"){
throw new InvalidInputException("Please, ...... cannot be Yes")
}

Hello @Florian Bonniec 

Thanks for that last input, though I don't follow.

Not familiar with use of the .name with getCustomFieldValue() - how does this work?

if(getCustomFieldValue(issue, CUSTOM_FIELD_ID).name

 Also, would it be best to add 2nd error message just after the 1st (for "None" selection) like so?

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

if(getCustomFieldValue(issue, CUSTOM_FIELD_ID).name == "Yes"){
throw new InvalidInputException("Response cannot be Yes")
}

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events