@mentions in automation smart values shows accountid instead of name
Asked by Jarlen Caden:

Hi, not sure if anyone has an answer to this, but hoping there is a workaround.

I currently use Jira automation for a lot of things, including sending emails based on triggered events. In one example, I will send the description of a newly created issue to my email address. Another example, I will send the body of a comment to a specific email.

In the first case, I use the {{issue.description}} smart value. If someone @mentions someone the description, it translates as \[\~accountid:xxxxxxxxxxxxx\] (xxx's are the account id) in the email that gets sent. Is there a way that instead we can show the user name instead of account ID? Does anyone know if this is possible?

Thanks!


Answers:
author: Paul Benati
body: All -- I've been trying to improve on the great work above, but to no avail until I stumbled upon this little gem: ***{{*** ***issue.comment.last.body.text}}***. This, at least for my use case, does the trick. I hope it works for you as well.

Reference: <https://support.atlassian.com/cloud-automation/docs/convert-wiki-markup-to-html-or-plain-text-in-jira-automation/>

comments: author: Jarlen Caden
body: [@Paul Benati](/t5/user/viewprofilepage/user-id/5175552) Thanks for sharing! That solution works and is exactly what I was looking for! I used ***{{issue.comment.last.body.++html++}}***and it not only shows the name, but it also translates all the HTML too, so that was an added bonus. Thanks again.


author: Darryl Lee
body: Yeah, it's kind of annoying that you can do the reverse (programmatically generate a mention from an Account ID), like so:

\[\~accountid:{{Squad leader.accountId}}\]

<https://support.atlassian.com/cloud-automation/docs/jira-smart-values-users/>

But I actually do think Automation is capable of this, albeit it is UGLY.

Here's how you might be able to do it:

1. Make a web request to grab user properties getting the Account ID of the @mention by using the **match()** function. (Note, this will be complicated if there are multiple mentions.)

2. The results from the web request are returned in a JSON object that can be referenced like {{webResponse.body.displayName}}

3. Finally, in the body of your email, you could do a substitution of the @mention with the accountID with the Display Name using the**replaceAll()**function.

This is all a little gnarly and hacky (especially deal with the case of multiple mentions - see below), but technically it should be possible.

Here's the reference for text functions I mentioned: [++Automation Text Field Functions++](https://support.atlassian.com/cloud-automation/docs/jira-smart-values-text-fields/)

And maybe, just maybe, you can iterate through what **match()** returns to handle multiple mentions? Hrm, but can you have more than one web request? How would you distinguish the separate results? Tricky.

There's discussion about of the **Advanced Branching** which *might* enable this here: [Branching over smart values in Jira Automation](https://community.atlassian.com/t5/Automation-articles/Branching-over-smart-values-in-Jira-Automation/ba-p/1741935?utm_source=atlcomm&utm_medium=email&utm_campaign=immediate_general_comment&utm_content=topic)

Hmm...[@Bill Sheboy](/t5/user/viewprofilepage/user-id/1685313) can you include Smart Values in a**replaceAll()** replacement strings?

Can we maybe set Smart Value "variables" and use those? Hum, wouldn't be able to change the variable names though (mention1, mention2, etc.) because I don't think variable names can be built from Smart Values... Or maybe they can?

Might be fun to try and see. :-}

comments: author: Bill Sheboy
body: Hi [@Darryl Lee](/t5/user/viewprofilepage/user-id/1365322) !

In my experience, use of smart values in functions is still inconsistently implemented. And rarely do created variables work as function parameters. The work-around is using replaceAll() and chained functions with substringAfter(), and so forth, to get at the desired values.


author: Jarlen Caden
body: Thanks [@Darryl Lee](/t5/user/viewprofilepage/user-id/1365322) [@Bill Sheboy](/t5/user/viewprofilepage/user-id/1685313)

This may work, but you're right, it gets complicated with multiple @ mentions (and it's rare, in my experience, that there is just one). I'll dive more into it and explore what can be done. I like the direction you both give though -- this may help lead me to the solution I'm looking for.


author: Darryl Lee
body: Heh yeah, I've gotten as far as extracting mention accountids and looking up the display names, but then I'm trying to generate an email body thusly and it's failing:
> {{issue.description.replace.("\~accountId:".mentionId,webResponse.body.displayName)}}

So I guess you're saying we'll need to do something like finding all the text up to the first mention (using a match), and then include that, then the display name of the first mention.... and then ooof, figure out all the text **between** that mention and the next mention, and then the next mention, and then anything **after** the last mention? Yuuuuuck. :-}


author: Jarlen Caden
body: LOL yup, exactly. The better solution is that it just displays name rather than account id from automation. Might have to do with the way the markdown translates in Jira. I think that caused issues in the past with how @ mentions were displaying on the page before too.


author: Darryl Lee
body: Augh. I'm so close.

![Screen Shot 2022-11-10 at 2.23.39 PM.png](https://community.atlassian.com/t5/image/serverpage/image-id/225239iD66D788DADB885E0/image-size/large?v=v2&px=999 "Screen Shot 2022-11-10 at 2.23.39 PM.png")

Advanced Branch to search for accountids:

```
{{issue.description.match("\[~accountid:(.+?)\]")}}
```

And then I can even replace the code for mention in a variable:
> {{body.replace(mentionId.concat("\]"),webResponse.body.displayName).replace("\[\~accountid:","")}}

BUT I'm running into the age-old problem of automation: everything runs in parallel! So I end up with this. (From my logs)
> Body: Darryl Lee and 5ffcb54xxxxxxxxxxxxxxxxx\] should know about this., Body: 55705xxxxxxxxxxxxx-xxxxxx\] and Darryl Test should know about this.

And ooof, the email? The updated {{body}} didn't make it there at all, because OOOF, parallel.

I tried putting the email outside of the IF block, and there {{body}} is completely blank, because hooray, variables are scoped locally to branches. <https://jira.atlassian.com/browse/JSWCLOUD-22527>

There's a clever hackaround for this, isn't there [@Bill Sheboy](/t5/user/viewprofilepage/user-id/1685313) It's escaping me at the moment though.


author: Bill Sheboy
body: I cannot think of a way to do this with multiple mentions in a rule...

One sticking point continues to be match() cannot use variable values (e.g. from another match result or a created variable) as the search string. If it could, the mentioned users' accountId values could be gathered and parsed into a OR expression as regEx, and then a single match could pull them all out of the REST API response for all possible users.

I also tried using doing this all with the REST API call, using accountId IN (*list of values*) with the user query REST API, but I couldn't figure out how to make the call work.


author: Darryl Lee
body: Oh neat idea about multiple results. This would be the call:
> <https://boomaster.atlassian.net/rest/api/3/user/>**bulk**?accountId=557058:xxxxxxxxxx\&accountId=5ffcb54xxxxxxxxxxxxxxxxx

But I still think the issue would be replacing the [@mention](/t5/user/viewprofilepage/user-id/732788) macros in the description text with the display names.

I don't think a single match() statement would... oh wait, if you \*chain\* multiple **replace()** statements together...

And you're not doing it in a loop...

Let me see...


author: Darryl Lee
body: Dangit Bill, ya led me down the same path again.

I can grab all the @mentions account Ids and Display names thusly:
> <https://boomaster.atlassian.net/rest/api/3/user/bulk?{{#issue.description.match>("\\\[\~accountid:(.+?)\\\]")}}accountId={{.}}{{\^last}}\&{{/}}{{/}}

And I can use that to generate a very nice set of **replacement()**functions thusly:
> {{#webhookResponse.body.values}}.replace("\[\~accountid:{{accountId}}\]","{{displayname}}"){{/}}

Which I can throw into a variable, and it looks lovely:
> .replace("\[\~accountid:55705xxxxxxx\]","Darryl Lee").replace("\[\~accountid:5ffcb5xxxxxx\]","Darryl Test")

But yeah, there's no way to *use* those functions on another smart value. I tried this:
> {{issue.description.replace(replacement)}}

And got nada. Which is weird, because you think I'd at least get an error or something about **replace()** not having enough parameters or something.

And of course, ***this won't work***:
> {{issue.description**{{#webhookResponse.body.values}}.replace("\[\~accountid:{{accountId}}\]","{{displayname}}"){{/}}**}}

If I try to publish that, I get:  
> Error parsing template: Mismatched start/end tags: null != in template-ca9d6824-3781-4046-aa24-7addd0e7873b:1

Hum.


author: Darryl Lee
body: Man... if only this worked:

```
{{issue.description.eval(replacement)}}
```

OH. I know why I'm not getting an error though. Automation must be interpreting my variable name, as well as "eval" as me looking for some additional property for **description** that doesn't exist.


author: Bill Sheboy
body: Hi [@Jarlen Caden](/t5/user/viewprofilepage/user-id/4653804)

Short answer: not with the way mentions are implemented.

One work-around is to call the REST API to get the user's information using the accountId from the mention, and then you have the displayName, emailAddress, etc.

If you want to try that, here is a how-to article for calling the REST API from a rule: <https://community.atlassian.com/t5/Jira-articles/Automation-for-Jira-Send-web-request-using-Jira-REST-API/ba-p/1443828>

And the method to call to get the user's information is: <https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-users/#api-rest-api-3-user-get>

Kind regards,  
Bill

comments: author: Jarlen Caden
body: Hey [@Bill Sheboy](/t5/user/viewprofilepage/user-id/1685313)

Thanks for the quick reply! I didn't know about the web request option -- that's pretty cool and something I may leverage in future for other things. For this, though, I would need to get that info and then search/replace accountid in description with the returned value from the web request. As good as automation is, I don't think it can do that level of string replacement.

I may have to put this in as a feature request maybe.

thanks,  
Jarlen


author: Bill Sheboy
body: You can use the [match() function](https://support.atlassian.com/cloud-automation/docs/jira-smart-values-text-fields/#match--) with a regular expression to get the accountId, and then pass that as a parameter for the REST API call.


Tags: jira-cloud

smart-value