Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.
×Hi,
I'm trying to create a new issue on a project that requires a value in a Select List (multiple choices) upon creation. I made a Script Runner Dynamic Form where I ask for simple text values:
- project key
- issue type
- reporter
- summary
- Brand -> custom field Select List (multiple choices)
I create a ComponentAccessor.issueService.newIssueInputParameters() with all the parameters, but when I try for the validateCreate() method I get this error:
java.lang.AssertionError: Errors: {customfield_12914=Invalid value '[MRP]' passed for customfield 'Brand/s'. Allowed values are: 14829[TON], 15901[MRP], 15725[TCF], -1}
Here's the script:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.IssueManager
import com.onresolve.scriptrunner.parameters.annotation.ShortTextInput
import com.onresolve.scriptrunner.parameters.annotation.UserPicker
import com.onresolve.scriptrunner.parameters.annotation.meta.Option
import org.apache.log4j.Logger
import org.apache.log4j.Level
import static Constants.*
// Set the logger
def lg = Logger.getLogger(getClass())
lg.setLevel(Level.DEBUG)
// Constants class
class Constants {
// Custom fields
static final int IDBrand = 12914
}
// INITIALIZATION
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
@ShortTextInput(description = "Project key e.g. JTC (mandatory)", label = "Project")
String projectKeyName
@ShortTextInput(description = "Issue Type name e.g. Bug (mandatory)", label = "Issue Type")
String issueTypeName
@UserPicker(description = "User that will be the reporter of the issue (mandatory)", label = "Reporter")
ApplicationUser reporter
@ShortTextInput(description = "Priority name e.g. Medium (optional, if empty default will be set)", label = "Priority")
String priorityName
@ShortTextInput(description = "Brand/s name e.g. TCF (depends on project & issue type)", label = "Brand/s")
String brandName
@ShortTextInput(description = "The summary for the new issue (mandatory)", label = "Summary")
String summary
@ShortTextInput(description = "The description for the new issue (optional)", label = "Description")
String description
def cFieldManager = ComponentAccessor.getCustomFieldManager()
def issueService = ComponentAccessor.issueService
def constantsManager = ComponentAccessor.constantsManager
IssueManager issueManager = ComponentAccessor.getIssueManager()
def project = ComponentAccessor.projectManager.getProjectObjByKey(projectKeyName)
assert project: "Could not find project with key $projectKeyName"
def issueType = constantsManager.allIssueTypeObjects.findByName(issueTypeName)
assert issueType: "Could not find issue type with name $issueTypeName"
// Reporter can't be empty
assert reporter : "Reporter can't be empty. Please select someone"
// if we cannot find the priority with the given name or if this is null, then set the default priority
def priority = constantsManager.priorities.findByName(priorityName) ?: constantsManager.defaultPriority
// Summary can't be empty
assert summary : "Summary can't be empty. Please fill it with something"
// CUSTOM FIELDS
// search for brand option
def cfBrand = cFieldManager.getCustomFieldObject(IDBrand)
def iss = issueManager.getIssueByCurrentKey("YFH-1667")
def availableOptions = ComponentAccessor.optionsManager.getOptions(cfBrand.getRelevantConfig(iss))
def optionToSet = availableOptions.findAll { it.value == brandName }
assert optionToSet: "Could not find option with value $newValue. Available options are ${availableOptions*.value}"
// set issue paramethers
def issueInputParameters = issueService.newIssueInputParameters().with {
setProjectId(project.id)
setIssueTypeId(issueType.id)
setReporterId(reporter.key)
setPriorityId(priority.id)
setSummary(summary)
setDescription(description)
addCustomFieldValue(IDBrand, optionToSet.toString())
}
def validationResult = issueService.validateCreate(loggedInUser, issueInputParameters)
assert validationResult.isValid(): validationResult.errorCollection
def result = issueService.create(loggedInUser, validationResult)
assert result.isValid(): result.errorCollection
"New issue key: $result.issue.key"
The line where I get the error is Bold
Thanks,
Stefano
Hi @Stefano Alessandrini (sono italiano anch'io, ma ti rispondo in inglese ^^)
unfortunately the version of Scriptrunner for Jira Cloud does not offer all the possibilities existing on the Jira Server.. in truth, if it were not for some shared scripted function to be included in the workflows to support validations, post-functions, etc, this app on Jira Cloud it is actually useless.
Make sure that the script you write is usable in Cloud first, but I can already tell you that certain types of scripts are simply not executable, if you are not on Server..
Bye,
Fabio
Ok thanks for the information, this is a problem... Scriptrunner is a must for every Jira Instance.
Despite this, we also have a Datacenter instance where I was able to set the Select list (Multiple choices) value
String[] brandNames = brandNames.tokenize(",")
dataToWrite = ComponentAccessor.optionsManager.getOptions(brandCustomField.getRelevantConfig(issueContext)).findAll { it.value in brandNames}*.optionId*.toString()
Then I could add this value as issue paramether with:
addCustomFieldValue(IDBrand, dataToWrite)
Thank you for the answer
Stefano
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Stefano Alessandrini You can use Dynamic Forms for Jira to set the field's value based on other field values or just create a default value.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Atlassian Government Cloud has achieved FedRAMP Authorization at the Moderate level! Join our webinar to learn how you can accelerate mission success and move work forward faster in cloud, all while ensuring your critical data is secure.
Register Now
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.