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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Groovy Script Error

I have a groovy script as a post function and I want to create a Sub-Task depending on whether the value of a custom field is "Yes". It is just a Yes or No custom field type.

Below are what I am currently importing and what my snippet of the script is

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField

import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.project.Project
import groovy.transform.Field
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
import com.atlassian.jira.config.SubTaskManager
import com.atlassian.jira.user.ApplicationUser



// Check for SAP Access
def sapTypeCF = customFieldManager.getCustomFieldObjectByName("Needs SAP Access?")
LazyLoadedOption sapTypeField =(LazyLoadedOption) parentIssue.getCustomFieldValue(sapTypeCF)
def valueOfSAPType = sapTypeField?.getValue()

I get a failed script execution with the error:

2020-12-08 15:20:59,056 ERROR [workflow.AbstractScriptWorkflowFunction]: Workflow script has failed on issue TPSHR-17868 for user 'Admin'. 
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '[Yes]' with class 'java.util.ArrayList' to class 'com.atlassian.jira.issue.customfields.option.LazyLoadedOption' due to: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: com.atlassian.jira.issue.customfields.option.LazyLoadedOption(com.atlassian.jira.issue.customfields.option.LazyLoadedOption) at Script61.run(Script61.groovy:57)

 

1 answer

1 accepted

1 vote
Answer accepted

Base on the error, my guess is that your custom field is either Checkbox or Multi-Select.

This means that result of getCustomFieldValue is an AarrayList<LazyLoadedOption> rather than a single LazyLoadedOption

What you can try instead is something like this:

def sapTypeCfValues = parentIssue.getCustomFieldValue(sapTypeCF)
def sapTypeYes = sapTypeCfValues.any{it.value == 'Yes'}

Ahh I see, small mistake on my side. The original custom field is a Select List type and I created the new custom field as a Checkbox. I just deleted it and re-created it as Select List and it worked now.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.13.1
TAGS
Community showcase
Posted in Jira Software

Presenting the "Best of 2020" Jira Software roundup!

Catch up with Atlassian Product Managers in our 2020 Demo Den round-up! From Advanced Roadmaps to Code in Jira to Next-Gen Workflows, check out the videos below to help up-level your work in the new ...

7,104 views 8 28
Join discussion

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you