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)
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.