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,295,867
Community Members
 
Community Events
165
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
Community showcase
Posted in Jira Service Management

Jira Service Management Documentation Opportunities

Hello everyone, Hope everyone is safe! A few months ago we posted an article sharing all the new articles and documentation that we, the AMER Jira Service Management team created. As mentioned ...

310 views 0 9
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you