Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,463,365
Community Members
 
Community Events
176
Community Groups

Add users to custom field based on conditions

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

0 votes
Answer accepted

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()

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events