Add users to custom field based on conditions

Daniel Alonso August 24, 2021

I need to add some users on a multi-user custom field based on the selection of a combo box.

Right now, I'm adding the users manually to test my code in the following way and it works well:

def issueKey = 'OOOO-1'

def customFieldName = 'Business Owner(s)'
def dashboardOwners = [accountId: '34534534535', accountId: '5345345345']


put("/rest/api/3/issue/" + issueKey)
.header("Content-Type", "application/json")
.body([
fields:[
customfield_12534 : [dashboardOwners]
]
])
.asString()

 But...

How can I add a new user into the 'dashboardOwners' variable?

I tried with

 

dashboardOwners.add(accountId: '346666')



But I got an error.

Any suggestions?

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Daniel Alonso August 26, 2021

Answering my own question:

def issueKey = issue.key

// Fetch the issue object from the key
def issue = get("/rest/api/2/issue/${issueKey}")
.header('Content-Type', 'application/json')
.asObject(Map)
.body

// Get all the fields from the issue as a Map
def fields = issue.fields as Map

// Get the Custom field to get the option value from
def customField = get("/rest/api/2/field")
.asObject(List)
.body
.find {
(it as Map).name == 'Dashboard Category'
} as Map

// Extract and store the option from the custom field
def checkboxValues = fields[customField.id] as List<Map>

// Get each of the values from the checkbox field and store them
def checkboxFieldValues = checkboxValues.collect {
it.value
}

//Define the Dashboard owners Array
def dashboardOwners = []

logger.info(checkboxFieldValues.toString())

if (checkboxFieldValues.contains("AR PL")) {
logger.info('AR')
dashboardOwners << [accountId: "608975b57f64f57"]
}
if (checkboxFieldValues.contains("Consumption")) {
dashboardOwners << [accountId: "60897921ba6e3b8"]
dashboardOwners << [accountId: "60890069915d1a"]
}
if (checkboxFieldValues.contains("BP")) {
dashboardOwners << [accountId: "608976a697570"]
}
if (checkboxFieldValues.contains("Distribution")) {
dashboardOwners << [accountId: "608976006ba6b143"]
}
if (checkboxFieldValues.contains("Global")) {
dashboardOwners << [accountId: "608970071317c14"]
}
if (checkboxFieldValues.contains("Production")) {
dashboardOwners << [accountId: "5e9975feb77bf40c1651809a"]
}
if (checkboxFieldValues.contains("Client")) {
dashboardOwners << [accountId: "608906864facb"]
}

//Update the Dashboard owners to the Issue

def customFieldName = 'Dashboard Business Owner(s)'

put("/rest/api/3/issue/" + issueKey)
.header("Content-Type", "application/json")
.body([
fields:[
customfield_12534 : dashboardOwners
]
])
.asString()
TAGS
AUG Leaders

Atlassian Community Events