Set environment custom field based on other fields
Using https://docs.adaptavist.com/sr4jc/latest/features/script-listeners or https://docs.adaptavist.com/sr4jc/latest/features/workflow-extensions/post-functions , it is easy to update Environment Custom Fields using other any issue information.
In this example, we have created 2 standard multi-select Jira fields:
“Applications”: to select the list of applications we want to book
“Categories”: to select the list of environment categories we want to book
Based on selected applications and categories, we want to automatically set the “Environments” custom field with the list of all matching environments to book.
Here is a script example you can adapt and re-use for that purpose:
def applicationsFieldKey = "customfield_12383"
def categoriesFieldKey = "customfield_12384"
def environmentsFieldKey = "customfield_12300"
def environmentsFieldAppKey = "apwide-environment-environments" // how to get this key: https://golive.apwide.com/doc/latest/cloud/environment-custom-fields#EnvironmentCustomFields-CreateanEnvironmentCustomField
def issueKey = issue.key
logger.info("IssueKey: $issueKey")
def projectId = issue.fields.project.id
logger.info("ProjectId = ${projectId}")
def currentUser = user
logger.info("Current user: $currentUser")
def selectedApplicationNames = issue.fields[applicationsFieldKey]*.value
logger.info("Selected application names: ${selectedApplicationNames}")
def selectedCategoryNames = issue.fields[categoriesFieldKey]*.value
logger.info("Selected category names: ${selectedApplicationNames}")
def environmentOptionValues = get("/rest/api/3/field/com.holydev.env.plugin.jira-holydev-env-plugin__${environmentsFieldAppKey}/option/suggestions/edit?projectId=${projectId}")
.asObject(Map).body.values
logger.info("Environments customfield option values: ${environmentOptionValues}")
def matchingEnvironmentOptions = environmentOptionValues.findAll {
optionValue -> selectedApplicationNames?.contains(optionValue.properties.application) && selectedCategoryNames?.contains(optionValue.properties.category)
}
logger.info("Matching environment options: ${matchingEnvironmentOptions}")
def matchingEnvironmentOptionIds = matchingEnvironmentOptions*.id
logger.info("Matching environment option ids: ${matchingEnvironmentOptionIds}")
def issueUpdateBody = [
fields: [
"${environmentsFieldKey}" : matchingEnvironmentOptionIds
]
]
logger.info("Issue update body: ${issueUpdateBody}")
def updatedIssueResult = put("/rest/api/3/issue/${issueKey}")
.header('Content-Type', 'application/json')
.body(issueUpdateBody)
.asString()
logger.info("Updated issue result: ${updatedIssueResult}")
For certain workflow post-functions, ScriptRunner enhances the script context by providing additional variables, which can be used to manipulate data.
For instance, in the "Create Sub-Task" post-function, a subtask
variable is provided to populate sub-task fields, and an issue
variable is available to read field values. However, the data structures between the issue fields and the sub-task fields differ, requiring conversion to extract field IDs:
subtask.fields.customfield_11649 = (issue.fields.customfield_11649 as List).collect { it.id }