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

Scriptrunner Merge Two User Lists and Populate on Issue

Dorian Workman April 3, 2020

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

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Dorian Workman April 5, 2020

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")

Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 6, 2020

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
Dorian Workman April 6, 2020

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

Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 7, 2020

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.

Dorian Workman April 8, 2020

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!

TAGS
AUG Leaders

Atlassian Community Events