Set value in Select List (multiple choices) field on create new issue script

Stefano Alessandrini July 15, 2021

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

1 answer

1 accepted

0 votes
Answer accepted
Reply Test Group July 19, 2021

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.

1.png

 

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

Stefano Alessandrini July 21, 2021

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

Like Reply Test Group likes this
Jarosław Solecki _Deviniti_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
April 17, 2023

@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. 

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
PERMISSIONS LEVEL
Site Admin
TAGS
AUG Leaders

Atlassian Community Events