Skip to main content
Skip table of contents

Use users set in environments to approve a Booking Request

Use Case

Imagine that we have the following situation

  • Our user names of our approvers are defined in an environment attribute called “Approvers” (separated by a comma when several approvers)

  • We use an “Approvers” multi-user select custom field in our Booking Requests to approve the transition

We would like to achieve the following:

  • When the requester selects an Environment in the Environment Custom Field “Environment(s) to Book”, we would like to automatically populate the “Approvers” custom field with all user names found in “Approvers” attribute of the environments to book

You can perform this by adding a Script Runner Listener or a Script Runner Post Function with following script. Example of a Listener:

This script will only work with of Golive v9.1.+

Main Script GOLIVE 9.1.+

Adapt this script to use the names of attributes and custom fields that are setup in your context.

GROOVY
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.user.ApplicationUser
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.apwide.env.api.GoliveAccessor
import com.atlassian.jira.component.ComponentAccessor

@WithPlugin("com.holydev.env.plugin.jira-holydev-env-plugin")
@PluginModule
GoliveAccessor goliveAccessor

def environmentCustomFieldName = "Environment(s) to book"
def environmentAttributeName = "Approver(s)"
def userNameDelimiter = ","
def multiUsersPickerCustomFieldName = "Approvers"

def getUserByName(String username) {
  try {
    return ComponentAccessor.userManager.getUserByName(username)
  }
  catch (Throwable e){
    return null
  }
}

def assignUsers(Issue issue, String multiUsersPickerCustomFieldName, List<ApplicationUser> users) {
  def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
  def customField = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName(multiUsersPickerCustomFieldName)?.find({ cf -> cf.isRelevantForIssueContext(issue) })
  if (customField) {
    issue.setCustomFieldValue(customField, users)
    issue = ComponentAccessor.issueManager.updateIssue(loggedInUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
  }
}

def environmentsToBookField = goliveAccessor.golive().environmentFields.getByName(issue, environmentCustomFieldName)
def environmentsToBook = environmentsToBookField.getValue()
def usernames = environmentsToBook.collect {environment ->
  return environment.attributes[environmentAttributeName]?.split(userNameDelimiter)
}.flatten()

List<ApplicationUser> users = usernames
    .collect { username -> getUserByName(username) }
    .findAll { user -> user != null }

assignUsers(issue, multiUsersPickerCustomFieldName, users)

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.