Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Automation: Add users mentioned in last comment to CC field

Marcin Kupisz January 21, 2025

Hi Jira People,

I have the following problem which I hope you can help me with.


I would like to create an automation that basically does 2 things:
1. when the comment is added to the issue, the automation checks if there are any users mentioned there.

2. if a single or multiple users are mentioned, every single one of them is added to CC field.

Why such automation? I've employed Issue Security Scheme based on that field. As many times my users are mentioning users in the comment rather than putting them in CC field I would like to automate this process so that everyone is notified when someone mentions them (also they don't really see the issue before they're added to the CC field)

 

Now I've been partially successful as I did manage to create and automation which is capable of checking if there is any user mentioned in the comment. The problem is, if there is more than one user mentioned, the automation adds just one, not all of them.

 

My current partially working setup is this:


The trigger is simple: When: Issue Commented

2025-01-21 15_05_21-Audit log - Automation - Jira — Mozilla Firefox.png

Then I'm checking if the body of the last comment contains mentions

2025-01-21 15_06_04-Audit log - Automation - Jira — Mozilla Firefox.png

Then I'm branching out with Advanced branching

2025-01-21 15_12_43-Rule builder - Automation - Jira — Mozilla Firefox.png

and for Smart Value

{{issue.comments.last.body.match("(\[~accountid:(.*?)\])").remove("[~accountid:").remove("]").asJsonObject("id")}}

I create a Variable smartUsers

 

Then Edit issue fields

2025-01-21 15_13_16-Rule builder - Automation - Jira — Mozilla Firefox.png

{ "update": { "CC" : [ { "add": {{smartUsers}} } ] } } 


I also added

2025-01-21 15_17_14-Rule builder - Automation - Jira — Mozilla Firefox.png

 

and in the audit log I can see multiple user IDs, however the CC field is populated with just one and it's driving me crazy. Please help!

 

1 answer

0 votes
Stefan Salzl
Community Champion
January 21, 2025

Hi @Marcin Kupisz 

According to this documentation using smart values in a multi-user-picker takes a jsonObjectArray 

A variable itself is always stored as a string. Therefore I would suggest to use the "asJsonObjectArray" (which you already used when creating the variable) in the real usage in your json (in your "Edit Issue" action)

Please let me know if this works.

Best
Stefan

Bill Sheboy
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.
January 21, 2025

Hi @Marcin Kupisz 

Yes, and...to the suggestions from @Stefan Salzl 

Rather than using the Advanced Branch, you may extract the mentioned users' account ID values and add them in one step, using inline iteration and dynamic JSON.  This will reduce the risk of errors / racetrack problems due to the parallel processing of such branching.

Also...the match() function can have difficulty with this scenario when there are line breaks in a comment.  I recommend using a split(" ") on the text before attempting the match:

{{issue.comment.last.body.split(" ").match("\[~accountid:(.*)\]").distinct}}

 

Kind regards,
Bill

Like Stefan Salzl likes this
Marcin Kupisz January 21, 2025

Thank you both for your suggestions, how would you recommend I rebuilt my entire solution then?

Bill Sheboy
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.
January 21, 2025

As a reminder, the Atlassian Community is a place for people to learn and collaborate, not to provide turnkey solutions for specific scenarios.  I recommend trying the suggestions offered, including reviewing the docs, maybe writing intermediate results to the audit log, etc., to learn what helps.

 

For more details to what Stefan and I suggested:

Your field appears to be a multiple-select user field, and so the array JSON format is needed: https://support.atlassian.com/cloud-automation/docs/advanced-field-editing-using-json/#Multi-user-picker-custom-field

 

Smart values are name, spacing, and case-sensitive.  And, they frequently do not exactly match the displayed name on the issue pages.  When an incorrect smart value is used, that returns as null, often leading to rule errors / problems.  I recommend pausing and using this how-to article to identify the correct smart value for your "CC" field, and perhaps the custom field ID, as needed: https://support.atlassian.com/cloud-automation/docs/find-the-smart-value-for-a-field/

 

Any list may be processed using the long-format or inline iteration.  To build the dynamic JSON, the long-format is better: https://support.atlassian.com/cloud-automation/docs/jira-smart-values-lists/#Combined-function-examples

 

The expression I provided earlier gives the unique, account ID values found in mentions.  Those may be iterated to create the dynamic JSON.  The example below is a fake custom field ID; please user your own value.

{
"update": {
"customfield_12346": [
{{#issue.comment.last.body.split(" ").match("\[~accountid:(.*)\]").distinct}}
{ "add": { "id":"{{.}}" } } {{^last}},{{/}}
{{/}}
]
}
}

In this expression, the {{.}} refers to the individual item found when iterating an untyped list (without attribute names), and a comma is conditionally added between the records added.

 

Like Stefan Salzl likes this
Marcin Kupisz January 21, 2025

Bill, I understand that this is not a 'give me all the answers straight away and solve my issue' place. I do appreciate pointing me in the direction of those manuals. I'm frustrated with a half working solution so hopefully with what you suggested I'll have a full solution. Thank you and Stefan one more time!

Like # people like this

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events