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,360,575
Community Members
 
Community Events
168
Community Groups

Scriptrunner Merge Two User Lists and Populate on Issue

Hi

I'm a complete coding and scriptrunner newbie so please forgive what is probably a basic question.

I am trying to create a Scriptrunner script that will populate an issue custom field with a distinct list of users pulled from two sources: a Jira user group and another multi-user custom field on the issue.

I have at least figured out how to pull in the user(s) in the user group and the user(s) in the issue custom field, but I can't figure out how to create a single consolidated list of unique users from those two sets, and then populate that list into another custom field.  I've gotten this far, can anyone help me with the rest please?

Thanks!

 

import com.atlassian.jira.component.ComponentAccessor

def groupManager = ComponentAccessor.getGroupManager()
def nc = groupManager.getUsersInGroup("new-calc-test")
def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject("customfield_12705")
def cFieldValue = issue.getCustomFieldValue(cField)

 

1 answer

1 accepted

0 votes
Answer accepted

Updated code below.  I've gotten to the point where I appear to be pulling in the users in the user group ("EMDs") and the users in the RM field on the issue ("RMs").  

What I need to do is merge those two user lists, removing any duplicates, and then add that merged list into customfield_12902 ("Union").

Can anyone help please?

Thanks!

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField

def EMDs = ComponentAccessor.getGroupManager().getUsersInGroup("new-calc-test")

def RM = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_12705")

def RMs = issue.getCustomFieldValue(RM)

def Union = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_12902")

Hi @Dorian Workman,

please try something like this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.security.groups.GroupManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager

final String emdUsersGroupName = "new-calc-test"
final String rmCustomFieldId = "customfield_12705"
final String unionCustomFieldId = "customfield_12902"

GroupManager groupManager = ComponentAccessor.getGroupManager()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
UserManager userManager = ComponentAccessor.getUserManager()

CustomField rmCustomField = customFieldManager.getCustomFieldObject(rmCustomFieldId)
CustomField unionCustomField = customFieldManager.getCustomFieldObject(unionCustomFieldId)

Collection<ApplicationUser> emdUsers = groupManager.getUsersInGroup(emdUsersGroupName)
Collection<ApplicationUser> rmUsers = issue.getCustomFieldValue(rmCustomField)
Collection<ApplicationUser> unionUsers = rmUsers + emdUsers

Collection<ApplicationUser> unionOldUsers = issue.getCustomFieldValue(unionCustomField)
unionCustomField.updateValue(null, issue, new ModifiedValue(unionOldUsers, unionUsers), new DefaultIssueChangeHolder())

I believe it is not necessary to remove duplicites, because it is not possible to have the same user twice in the user picker field.

Like # people like this

Thank you for the reply @Hana Kučerová!

This almost worked, except I am getting a static type checking error on lines 23 and 25 (same error on both lines) - see attached screenshot.  Any suggestions please?

Thanks again!

Error.png

Hi @Dorian Workman ,

I believe it doesn't matter much, you still should be able to save the script and test it. The problem is in the getCustomFieldValue method - the type of the returned data depends on the type of the custom field.

But I've tried to improve it to get rid of the message:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.security.groups.GroupManager
import com.atlassian.jira.user.ApplicationUser

final String emdUsersGroupName = "new-calc-test"
final String rmCustomFieldId = "customfield_12705"
final String unionCustomFieldId = "customfield_12902"

GroupManager groupManager = ComponentAccessor.getGroupManager()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

CustomField rmCustomField = customFieldManager.getCustomFieldObject(rmCustomFieldId)
CustomField unionCustomField = customFieldManager.getCustomFieldObject(unionCustomFieldId)

Collection<ApplicationUser> emdUsers = groupManager.getUsersInGroup(emdUsersGroupName)
Collection<ApplicationUser> rmUsers = issue.getCustomFieldValue(rmCustomField) as Collection<ApplicationUser>
Collection<ApplicationUser> unionUsers = rmUsers + emdUsers

Collection<ApplicationUser> unionOldUsers = issue.getCustomFieldValue(unionCustomField) as Collection<ApplicationUser>
unionCustomField.updateValue(null, issue, new ModifiedValue(unionOldUsers, unionUsers), new DefaultIssueChangeHolder())

 I've tested both the versions as the postfunctions in the workflow and the script worked for me.

Please let me know. Thank you.

My apologies @Hana Kučerová , I was testing out the first script in a scripted field, not in a post-function.  I added the second script to a post function and although one warning was thrown by the script editor, it worked perfectly!  Thank you so much for your help, it's greatly appreciated!

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira

Online AMA this week: Your project management questions answered by Jira Design Lead James Rotanson

We know that great teams require amazing project management chops. It's no surprise that great teams who use Jira have strong project managers, effective workflows, and secrets that bring planning ...

196 views 1 6
Read article

Atlassian Community Events