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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

Admins, notify your Jira instance of system-wide changes with the new admin announcement banner

Hi All! We’re excited to share the launch of an announcement banner that lets Jira site administrators communicate directly to their users across their  Jira Cloud instance.  ...

607 views 15 18
Read article

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