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

Script for copying Assignee Group Members into multi user picker field

Jakeer April 18, 2024

Hi Team,

Can you plz help me with a script for copying Assignee Group members into a multi user picker field.
When Assignee Group= Invoice, then all the members in the Invoice Group needs to be copied into a muti user picker custom field. Similarly if Group(Organization)= ICICI then all the members in the group ICICI needs to be copied in a multi user picker custom field.

Here Assignee Group group is a "Select Single Choice" Custom field and Group(Organization) is a "Group Picker" custom field.

Note: We need this script in Jira Cloud Instance.

I am using the below script, but getting error-

import groovy.json.JsonSlurper
import groovy.json.JsonOutput
try {
    def issueKey = 'TP1-2'
    def issueDetails = get("/rest/api/2/issue/${issueKey}")
        .header('Content-Type', 'application/json')
        .asString()
    if (issueDetails.status != 200) {
        throw new Exception("Failed to retrieve issue details. Status code: ${issueDetails.status}")
    }
    def jsonResponse = new JsonSlurper().parseText(issueDetails.body) as Map
    def fields = jsonResponse.fields as Map
    if (fields.containsKey('customfield_10072')) {
        def groupName = fields.customfield_10072
        def groupNames = groupName instanceof List ? groupName : [groupName] // Ensure it's treated as a list
        def allGroupMembers = [] // Collect all group members here
        groupNames.each { singleGroupName ->
            def groupMembersResponse =
get("https://jacksparrow1.atlassian.net/rest/api/3/group/member?groupname=${singleGroupName}")
                .header('Content-Type', 'application/json')
                .asString()
            if (groupMembersResponse.status != 200) {
                throw new Exception("Failed to retrieve group members for group ${singleGroupName}. Status code:
${groupMembersResponse.status}")
            }
            def groupMembersJson = new JsonSlurper().parseText(groupMembersResponse.body) as Map
            def groupMembers = groupMembersJson?.values()?.flatten()?.collect { it['displayName'] } as Set
            allGroupMembers.addAll(groupMembers)
        }
        def currentUsers10071 = (fields.customfield_10071 ?: []) as Set
        currentUsers10071.addAll(allGroupMembers)
        def jsonBody = [
            fields: [
                customfield_10071: currentUsers10071.toList()
            ]
        ]
        def result = put("/rest/api/2/issue/${issueKey}")
            .header('Content-Type', 'application/json')
            .body(JsonOutput.toJson(jsonBody))
            .asString()
        if (result.status == 204) {
            return 'Success'
        } else {
            throw new Exception("Failed to update issue. Status code: ${result.status}, Response:
${result.body}")
        }
    } else {
        throw new Exception('customfield_10072 not found in the issue details')
    }
} catch (Exception e) {
    return "An error occurred: ${e.message}"
}

Thanks,

0 answers

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
PERMISSIONS LEVEL
Site Admin
TAGS
AUG Leaders

Atlassian Community Events