Listener to add each commentor (except author and assignee) to participants

Admin SP January 28, 2020

Hello,

we need to add each commentor to the Participants (we have custom field for this) in the issue.

As I know it can be done using Listeners in a ScriptRunner plugin.

Now script is like

import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.issue.IssueEvent

def issue = event.issue as MutableIssue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObject(10600)

def lastcomment = event.getComment()
def commentAuthor = lastcomment.getAuthorApplicationUser()

issue.setCustomFieldValue(cf, commentAuthor)

 

It has no any errors, but commentors don`t appear in a Participants.

What`s wrong in the script ?

 

Thank you for your answers and help!

1 answer

0 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 28, 2020

Is the "Participants" field the one from the Jira toolkit, or is it just a multi-user select field?

Admin SP January 28, 2020

Hi, Nic.

Participants is multi-user select field.

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 28, 2020

Ok, great, that's completely clear and explains the problem.

The value of a multi-user field is not a single user, it is a list of users.   You need to get the current list, add to it and post it back.  Not set it to a single user.  Even if it could accept a single user (it can't, it needs a list, even if that list is 0 or 1 items long), that would wipe out the current list.

Admin SP January 30, 2020

Hi, Nic.

Thank you for your advice.

Now it works

import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.util.IssueChangeHolder
import com.atlassian.jira.user.ApplicationUser

IssueChangeHolder changeHolder = new DefaultIssueChangeHolder()
def issue = event.issue as MutableIssue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObject(10600)

def lastcomment = event.getComment()
def commentBody = lastcomment.getBody()
def commentAuthor = lastcomment.getAuthorApplicationUser()

List<ApplicationUser> participants = [];
if (commentAuthor != issue.reporter){
ApplicationUser user = commentAuthor
participants.add(user);
}
if (commentAuthor != issue.assignee){
ApplicationUser user = commentAuthor
participants.add(user);
}
if (commentAuthor == issue.reporter){
return 0
}
if (commentAuthor == issue.assignee){
return 0
}

cf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf), participants), changeHolder)

It could be more "code friendly", but it works like expected.

May be it will be useful for someone.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events