Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Update custom fields with ScriptRunner based on the group the reporter belongs to

Kerli Suve
Community Champion
September 15, 2025

I have "Countries" field that I want to fill based on the reporters group, either when work item is created or the value for reporter has changed and I have to do it with ScriptRunner due to the amount of tickets created and Jira Automation limits. The field must be editable later manually when needed. 

I created an event listener for that but I am struggling with making the script work.

I can fill the "Countries" field but right away when I add a condition to check reporters group, i get some errors. This seems such an easy script so I am sure, someone in here could make this work in seconds. 

In Jira Automation it is pretty straight forward: 

  1. When field value changes for Reporter
  2. IF
    1. Reporter is in group "ExampleGroupA"
    2. Edit "Countries" to "ExampleCountryA"
  3. ELSE-IF 
    1. Reporter is in group "ExampleGroupB"
    2. Edit "Countries" to "ExampleCountryB"

... and so on.

2 answers

2 votes
Kerli Suve
Community Champion
September 15, 2025

Got it to work like intended but probably could be made easier or more logical. 

//Groups to check

final A_GROUP_NAMES = ["ExampleGroupA"]

final B_GROUP_NAMES = ["ExampleGroupB"]

//Get the issue from the event

def eventIssue = Issues.getByKey(issue.key as String)

//Check if reporter has changed

Map reporterChange = changelog?.items.find { eventIssue.reporter } as Map

//If it has not, then log

if (!reporterChange) {

    logger.info("Reporter was not updated on ${eventIssue.key}")

    return;

}

//If it did change

if (reporterChange) {

    //Define userlist from Each group

    def userListFromGroupA = []

    def userListFromGroupB = []

//For each group name, get their users and add to UserListFromGroup

    A_GROUP_NAMES.each { groupName ->

        def groupMembersA = Groups.getByName(groupName).getMembers()

        userListFromGroupA.addAll(groupMembersA)

    }

   

    B_GROUP_NAMES.each { groupName ->

        def groupMembersB = Groups.getByName(groupName).getMembers()

        userListFromGroupB.addAll(groupMembersB)

    }

    def reporterInGroupA = userListFromGroupA.findAll {user ->

        user['accountId'] == eventIssue.reporter.accountId}.collect { user ->}

    def reporterInGroupB = userListFromGroupB.findAll {user ->

        user['accountId'] == eventIssue.reporter.accountId}.collect { user ->}

       

        if (!reporterInGroupA & !reporterInGroupB) {

            logger.info("Reporter ${eventIssue.reporter.displayName} is not in the target group")

            return

        }

        def countriesFieldName = "Countries"

       

        if (reporterInGroupA) {

                    eventIssue.update {

            setCustomFieldValue(countriesFieldName, "ExampleCountryA")

        }

        logger.info("Countries field updated to ExampleCountryA for ${eventIssue.Key}")

           

        }

        if (reporterInGroupB) {

                    eventIssue.update {

            setCustomFieldValue(countriesFieldName, "ExampleCountryB")

        }

        logger.info("Countries field updated to ExampleCountryB for ${eventIssue.Key}")

    }

}
0 votes
Trudy Claspill
Community Champion
September 15, 2025

Hello @Kerli Suve 

It is a bit challenging to suggest what might be causing the errors you are getting without more information.

Please share with us the script you have written.

Please also share with us the errors you are getting.

Kerli Suve
Community Champion
September 15, 2025

At first I didn't receive reporter, it was always "empty" but now I was able to fix that issue. 

Then I noticed that the Countries was changed, even if the reporter was not in that group. But I believe I managed to fix that too. 

The code was with only one group and country for testing but now I have to edit the code to set all the possible groups and their relationship with the country, I will work on that and give an update here but this is my code currently and any ideas are still appreciated. 

//Groups to check

final GROUP_NAMES = ["ExampleGroupA"]

//Get the issue from the event

def eventIssue = Issues.getByKey(issue.key as String)

//Check if reporter has changed

Map reporterChange = changelog?.items.find { eventIssue.reporter } as Map

//If it has not, then log

if (!reporterChange) {

    logger.info("Reporter was not updated on ${eventIssue.key}")

    return;

}

//If it did change

if (reporterChange) {

    //Add users to list if they are in group

    def userListFromGroup = []

//For each group name, get their users and add to UserListFromGroup

    GROUP_NAMES.each { groupName ->

        def groupMembers = Groups.getByName(groupName).getMembers()

        userListFromGroup.addAll(groupMembers)

    }

    def reporterInGroup = userListFromGroup.findAll {user ->

        user['accountId'] == eventIssue.reporter.accountId}.collect { user ->}

       

        if (!reporterInGroup) {

            logger.info("Reporter ${eventIssue.reporter.displayName} is not in the target group")

            return

        }

        def countriesFieldName = "Countries"

        eventIssue.update {

            setCustomFieldValue(countriesFieldName, "ExampleCountryA")

        }

        logger.info("Countries field updated to ExampleCountryA for ${eventIssue.Key}")

}
 

 

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
TAGS
AUG Leaders

Atlassian Community Events