How can I add users to a custom field when they are tagged in a comment?

Daniel Nelson November 3, 2022

We have a Jira project where the users permitted to access each ticket are stored in a "Permitted Users" field, which is a multi-select user field. I'd like to automate how this field gets populated as follows:

  1. When a comment is added to the Jira issue, detect any users who have been tagged in the comment body.
  2. Add these users to the custom field, thereby permitting them access to the issue.

The alternative is adding the users to this field manually, but unfortunately people often forget to do so - they assume (as with many other information systems) that tagging a user is sufficient to send them a notification / grant them access.

This seems like a simple workflow, but I haven't been able to find a way to pick out the users who have been tagged in the comment body, nor to specify that the field value should be updated rather than replaced with the new values.

How can I achieve this?

5 answers

1 accepted

3 votes
Answer accepted
Luis Machado
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 4, 2022

Hi @Daniel Nelson

I think I've come up with a solution for you. I got the following rule to work for me in this manner using the Approvers field (multi-user picker), you'd just have to swap that out for your custom field.

Screen Shot 2022-11-04 at 11.40.17 AM.png

The rule triggers on every comment added to the issue and dumps the body of the comment into a variable {{CommentBody}}.

Screen Shot 2022-11-04 at 11.39.50 AM.png

It then runs a branch that parses the created variable for the actual account Ids, puts them into a list, and then edits the issue adding them to the field for each value in the list {{CommentUserList}}.

Screen Shot 2022-11-04 at 11.40.32 AM.png

Screen Shot 2022-11-04 at 11.40.39 AM.png

Hopefully this works for you, I didn't test with a custom field so I'm not sure if there a difference in how the values are added to it.

Daniel Nelson November 7, 2022

Hi @Luis Machado - this is great, thank you so much! I was able to use your answer to achieve the exact workflow I was looking for.

Like Luis Machado likes this
Younes Abou Khzam June 5, 2023

Hello,

 

I was looking at this solution, the issue would be that using permissions schemes, if the user cannot view the issue, then they cannot be @mentioned in it. thus, when someone types another users's name in the comment, they will not see it in the suggestions

Tran Ngoc Duc February 19, 2024

Hi @Luis Machado, I applied your configuration to the automation that adding users mentioned in the comment to a custom field named "Paricipants", but the automation was not working as expected when adding multiple users.

 

From the following screenshot, you can see that the automation added the user 1, and then replaced it with the user 2, and then replaced user 2 with user 3

 image.png

 

How can I add all 3 users? The automation configuration is as below

image.png

 

Thank you!

Ganesh Satav March 10, 2024

Hi Luis Machado,

How can this be achieved for Data Center version?

 

Luis Machado
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 14, 2024

@Tran Ngoc Duc - It seems the notification for your mention got lost in my mail, my apologies.

The List variable created '{{CommentUserList}}' should contain the full list of users mentioned in the comment and add them all to the field value. I rebuilt the rule in my test instance to make sure it still works since it has been some time.

I would suggest creating two log actions to help debug the values of the smart fields, something like this:

Screenshot 2024-03-15 at 12.25.22 AM.png

 

That way you can check the audit logs and ensure the values are populating correctly. The first variable would be something like "[~accountid:<id 1>] [~accountid:<id 2>] [~accountid:<id 3>]" while the second variable would be "id1, id2, id3"

Luis Machado
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 14, 2024

@Ganesh Satav  - I believe this same logic could be applied to Data Center. I'm not as well versed with that context though admittedly. I know DC has smart values but I think the functionality available largely depends on which version of Jira you're running.

Ben Radlinski May 2, 2024

The Advanced Branching part of the automation is a Cloud only feature as far as I can tell. Has anyone come up with a solution for Data Center? I'm still looking.

0 votes
Vamsi Krishna Panthulu March 7, 2024

@Daniel Nelson Can you help me to understand how the "permitted user" custom field is able to control access to that jira ticket.  Does it also need to rely on the permission schemes ?

 

I have a jira project and i want to restrict access to the tickets created in this project based on a custom field (customer name).  If a ticket is created for a particular customer, then only a set of users should be able to view or work on the issue.

0 votes
James_Gigantes November 3, 2022

I can get you most of the way there ... you will need to run the trigger on all comments, with an if else block and you will have to see if the comment body contains an account id based on the users that you would like to add to the custom field. Then you will have to edit the ticket, using some json. You will have to know the name of the custom field. Here is a link to provide some more information. https://support.atlassian.com/cloud-automation/docs/advanced-field-editing-using-json/ let me know how you do, I may be able to help more. 

comment body contains users.JPG

Daniel Nelson November 4, 2022

@James_Gigantes - thanks for your suggestion!

This setup would work for specific users, but not for an arbitrary tagged user. For example, I might take User1, User2, or User1000 (or multiple of them) in a comment - and it unfortunately isn't viable to add a separate Jira automation rule for every single user :)

To clarify my underlying requirement: I would like "user X is tagged" to translate into "user X has access to the ticket", where X could be any user. This seems like quite a simple workflow, perhaps I'm missing something where I should be using a setup other than a custom field for individual-issue security level inclusions like this?

0 votes
Rafael Costa
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.
November 3, 2022

Have You myGroovy plugin in your Jira instance? I would know to do it by myGroovy.

Daniel Nelson November 4, 2022

@Rafael Costa - could you let me know what myGroovy config you would use? (See my comment above to James for the specific requirement.)

Like Rafael Costa likes this
Rafael Costa
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.
November 5, 2022

well, I would set a listener or would add a scripted field in the issue screen with the follow script:

import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueInputParametersImpl
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue

issue = issue as MutableIssue
def comments = commentManager.getComments(issue)
def changeHolder = new DefaultIssueChangeHolder()
def issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService)
def issueManager = ComponentAccessor.getIssueManager()
def issueService = ComponentAccessor.getIssueService()
def cfManager = ComponentAccessor.getCustomFieldManager()
def commentManager = ComponentAccessor.commentManager

if (comments){
def changed = false
def customfield = cfManager.getCustomFieldObject("customfield_23013")
def customfieldValue = issue.getCustomFieldValue(customfield) as List
for (comment in comments){
if (!(comment.authorUser in customfieldValue)){
customfieldValue.add(comment.authorUser)
changed = true
}
}

if (changed){
customfield.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customfield), customfieldValue), new DefaultIssueChangeHolder())
issueIndexingService.reIndex(issue)
}
}

This would solve your case. This is a configuration to a groovy scripted field, if you want a listener, you would need set to run it each issue update. Maybe would be interesting some parameters to validate which issue type before run entire script, if you don't need run it to all issue types of your project. 

0 votes
Lewis Tanguhwar
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 3, 2022

@Daniel Nelson,

I doubt there is any built-in functionality that supports that.

Best regards

Suggest an answer

Log in or Sign up to answer