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:
... and so on.
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}")
}
}
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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}")
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.