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

Copy username from comment to User picker field

Sal May 10, 2022

Hello Everyone,

I would like to create Project automation where we can copy the username when a comment is added to a user picker field.

Any idea how this can be done?

BR,

2 answers

1 accepted

1 vote
Answer accepted
Andrea Pannitti
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 11, 2022

Hi @Sal ,

I copied my reply in this separated answer; so, if it's ok for you, you can accept this. Thank you.

 

let be "Users in last comment" the user picker (multi-user) that we want fill with users in last comment.
Then add a new custom Script Listener that starts on "Issue Commented" event and set in Script this code:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.user.ApplicationUser

def issue = event.issue

def cfUsersInLastComment = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName("Users in last comment").first()

def comments = ComponentAccessor.commentManager.getComments(issue)

String strComment = comments.last().body

List<String> userNames = strComment.findAll("\\[~.*?\\]")

def users = new ArrayList<ApplicationUser>()

userNames.each {users.add(ComponentAccessor.userManager.getUserByName(it.substring(2, it.length()-1)))}

cfUsersInLastComment.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cfUsersInLastComment), users), new DefaultIssueChangeHolder())

So, when a new comment is added to the issue, users in comment will be added to the user picker field.

Note: Each User in comments must be written as: @username (by selecting the one you want)

Sal May 16, 2022

Hello @Andrea Pannitti,

Sorry, I was away for a few days. This is working as you said. But my scenario is a little different.

It is like I have a single select field and it should only populate when there is some specific word in a comment such as "additional" then "username". It will pick the username from the comment to the single select field and delete the comment.

I am new to groovy and not sure about the changes that can be done here.

Thanks in advance!

Andrea Pannitti
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 16, 2022

Hi @Sal ,

I'll answer you by points:

  1. To identify the users entered in the comment, the users must necessarily be entered as I said in my previous answer, otherwise there could be problems in finding them.
  2. Checking if the comment contains a certain word is not a problem but for what has been said in point 1 it should not be necessary
  3. Deleting the comment is also not a problem

Thus, it seems that the proposed solution lacks only the deletion of the comment.
If not, what is wrong for you?
I would ask you to be as precise as possible in order to clarify what you need.

Thanks

Sal May 16, 2022

Hello @Andrea Pannitti,

When using a multi-user picker field it's working fine but I am not able to use this script for a single user picker field.

BR,

Andrea Pannitti
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 16, 2022

Hi @Sal ,

I think your problem depends on the fact that from the comment are extracts all users and therefore you have a list of users.
If so, try changing the last line of the previous code as follows:

cfUsersInLastComment.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cfUsersInLastComment), users.first()), new DefaultIssueChangeHolder())
Sal May 17, 2022

Hello @Andrea Pannitti

This is working as expected. Thank you very much for your help!

BR,

0 votes
Brant Schroeder
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 10, 2022

@Sal Are you coping the username from the individual who created the comment?  If so you would need to get the comment author using the following smart value {{comment.author.displayName}}   This could then be added to a custom field.  I am not sure if you will be able to set a user picker field with it.  You would have to try.

Sal May 10, 2022

@Brant Schroeder Sorry, I was not clear. When a user comments on a ticket with something like "add username" then the username should be added to a user picker custom field and delete the comment.

Andrea Pannitti
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 11, 2022

Hi @Sal,

this issue can be solved by ScriptRunner. Have you ScriptRunner?

Sal May 11, 2022

@Andrea Pannitti I have script runner and automation for jira  as well.

Andrea Pannitti
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 11, 2022

Ok @Sal ,
let be "Users in last comment" the user picker (multi-user) that we want fill with users in last comment.
Then add a new custom Script Listener that starts on "Issue Commented" event and set in Script this code:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.user.ApplicationUser

def issue = event.issue

def cfUsersInLastComment = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName("Users in last comment").first()

def comments = ComponentAccessor.commentManager.getComments(issue)

String strComment = comments.last().body

List<String> userNames = strComment.findAll("\\[~.*?\\]")

def users = new ArrayList<ApplicationUser>()

userNames.each {users.add(ComponentAccessor.userManager.getUserByName(it.substring(2, it.length()-1)))}

cfUsersInLastComment.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cfUsersInLastComment), users), new DefaultIssueChangeHolder())

So, when a new comment is added to the issue, users in comment will be added to the user picker field.

Note: Each User in comments must be written as: @username (by selecting the one you want)

Sal May 11, 2022

Thank you @Andrea Pannitti !

I will test this on my instance.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events