Copy username from comment to User picker field
Asked by Sal:

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,


Answers:
author: Andrea Pannitti
body: Hi [@Sal](/t5/user/viewprofilepage/user-id/4026907) ,

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)

comments: author: Sal
body: Hello [@Andrea Pannitti](/t5/user/viewprofilepage/user-id/2059144),

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!


author: Andrea Pannitti
body: Hi [@Sal](/t5/user/viewprofilepage/user-id/4026907) ,

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


author: Sal
body: Hello [@Andrea Pannitti](/t5/user/viewprofilepage/user-id/2059144),

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,


author: Andrea Pannitti
body: Hi [@Sal](/t5/user/viewprofilepage/user-id/4026907) ,

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())
```


author: Sal
body: Hello [@Andrea Pannitti](/t5/user/viewprofilepage/user-id/2059144)

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

BR,


author: Brant Schroeder
body: [@Sal](/t5/user/viewprofilepage/user-id/4026907) 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.

comments: author: Sal
body: [@Brant Schroeder](/t5/user/viewprofilepage/user-id/769100) 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.


author: Andrea Pannitti
body: Hi [@Sal](/t5/user/viewprofilepage/user-id/4026907),

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


author: Sal
body: [@Andrea Pannitti](/t5/user/viewprofilepage/user-id/2059144) I have script runner and automation for jira as well.


author: Andrea Pannitti
body: Ok [@Sal](/t5/user/viewprofilepage/user-id/4026907) ,  
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)


author: Sal
body: Thank you [@Andrea Pannitti](/t5/user/viewprofilepage/user-id/2059144) !

I will test this on my instance.


Tags: confluence-server

server