How do I add docx attachments to an e-mail with Automation for JIRA
Asked by E Hanumanthu:

Is it possible to add only docx attachments to an e-mail with Automation for JIRA ?

I am able to attach All the attachments by using below. But only the requirement is needs to add docx attachments. Can anyone help me out how to filter docx files from this.

Attachments:

\<ul\>

{{#issue.attachment}}

\<li\>\<a href="{{content}}"\>{{filename}}\</a\>\</li\>

{{/issue.attachment}}

\</ul\>


Answers:
author: Darryl Lee
body: Hi [@E Hanumanthu](/t5/user/viewprofilepage/user-id/5009140) - I think this will do the trick. I had to put it all on one line to avoid Automation adding line breaks in the email:
> \<ul\>  
> {{#issue.attachment}}{{#if(exists(filename.match("(.\*\\.docx)$")))}}\<li\>\<a href="{{content}}"\>{{filename}}\</a\>\</li\>{{/}}{{/}}  
> \</ul\>

Basically we are trying to **match** filenames that end with .docx, and **if** so, then include them.

I would also suggest that if you aren't already you should probably do a check to make sure that this smart value:
> {{#issue.attachment}}{{#if(exists(filename.match("(.\*\\.docx)$")))}}{{filename}}{{/}}{{/}}

**does not equal Empty**. (To ensure that you don't send an email if there are not any docx attachments.)

++**References:**++

* [++Jira smart values - text fields++](https://support.atlassian.com/cloud-automation/docs/jira-smart-values-text-fields/)
* ++[Jira smart values - conditional logic](https://support.atlassian.com/cloud-automation/docs/jira-smart-values-conditional-logic/)++

comments: author: Gnana Sekhar Reddy
body: Hi [@Darryl Lee](/t5/user/viewprofilepage/user-id/1365322)

Is it possible to add Particular file name in attachments to an e-mail with Automation for JIRA


author: Darryl Lee
body: Hi [@Gnana Sekhar Reddy](/t5/user/viewprofilepage/user-id/5015343) - so if you're asking if you can only include a specific file to the email, yes you would simply change the **match()** statement above to something like:

... **.match("(nameofyourfile\\.docx)")**

The \\ in front of the . is used because . is a "wildcard" character in regular expressions.


author: Gnana Sekhar Reddy
body: Hi [@Darryl Lee](/t5/user/viewprofilepage/user-id/1365322)

But when i am implementing that, getting below error in Audit log(Project Automation), E-mails notification not receiving . Can you help me out from this.

Note: This is file name : MOP-230_Test_1

Error rendering smart-values when executing this rule:  
Tokens must be separated by '.': "))): Hi {{issue.approver.displayname}}, {{issue.reporter.displayname}} has been submitted MOP for your Approval. Please approve it. Thank you. The issue: \<a href="{{issue.url}}"\>{{issue.key}} - {{issue.summary}}\</a\> Attachments: \<ul\> {{#issue.attachment}}{{#if(exists(filename.match("(MOP\\.docx)")))}}\<li\>\<a href="{{content}}"\>{{filename}}\</a\>\</li\>{{/}}{{/}} \</ul  
![Automation Error.jpg](https://community.atlassian.com/t5/image/serverpage/image-id/231585i0E5458A5D4975E7E/image-size/large?v=v2&px=999 "Automation Error.jpg")


author: Darryl Lee
body: Hi [@Gnana Sekhar Reddy](/t5/user/viewprofilepage/user-id/5015343) -- thank you for including the error message. It was very helpful in finding the problem.

The thing that was breaking your rule is that your quotation marks in the match block are ["curly quotes", not "straight" quotes](https://unix.stackexchange.com/questions/704762/what-are-curly-quotes-and-can-i-use-them-in-my-code).

I replaced the quotes with straight quotes, and it got rid of the error.

HOWEVER your rule will not work as intended unless you add a wildcard (.\*) to your match, so it should be:
> match("(MOP**.\***\\.docx)")

Also, as with my advice to [@E Hanumanthu](/t5/user/viewprofilepage/user-id/5009140) you probably want to do a check to ensure that there is an attachment that matches what you expect before sending the email.

![Screen Shot 2022-12-21 at 9.53.55 AM.png](https://community.atlassian.com/t5/image/serverpage/image-id/231613i4562D949C1E6D44A/image-size/large?v=v2&px=999 "Screen Shot 2022-12-21 at 9.53.55 AM.png")


author: E Hanumanthu
body: Hi [@Darryl Lee](/t5/user/viewprofilepage/user-id/1365322) Thank you so much for your valuable information.

*** ** * ** ***


author: Gnana Sekhar Reddy
body: Hi [@Darryl Lee](https://community.atlassian.com/t5/user/viewprofilepage/user-id/1365322) Thank you so much for your valuable information.

Now I have one more requirement. From this below how can we take only latest version document. Just i want latest one i don't want all the documents with that particular name. Can you please confirm me how can we do that.

Note: Documents should be likes this MOP-230_Test_1, MOP-230_Test_2, MOP-230_Test_3.......etc

\<ul\>  
{{#issue.attachment}}{{#if(exists(filename.match("(MOP.\*\\.docx)")))}}\<li\>\<a href="{{content}}"\>{{filename}}\</a\>\</li\>{{/}}{{/}}  
\</ul\>


author: Darryl Lee
body: Hi [@Gnana Sekhar Reddy](/t5/user/viewprofilepage/user-id/5015343) will any attached documents **not** be MOP documents?

If they are **all** MOP docs, then you could just grab the **.last** attachment:
> \<ul\>  
> \<li\>\<a href="{{issue.attachment.last.content}}"\>{{issue.attachment.last.filename}}\</a\>\</li\>  
> \</ul\>

But I guess if there might be other documents interspersed amongst the MOP docs. Hrm.

This will work:

* Create variable **allfilenames** that generates a comma-delimited list of MOP filenames, in order of attachment:{{#issue.attachment}}{{#if(exists(filename.match("(MOP.\*\\.docx)")))}}{{filename}}{{\^last}},{{/}}{{/}}{{/}}
* Create variable **allcontents** that generates list of MOP content links, in order of attachment:{{#issue.attachment}}{{#if(exists(filename.match("(MOP.\*\\.docx)")))}}{{content}}{{\^last}},{{/}}{{/}}{{/}}
* Update email to use **split** function to separate out lists of MOP filenames/contents, then use **last** function to get the last one.\<ul\>\<li\>\<a href="{{allcontents.split(",").last}}"\>{{allfilenames.split(",").last}}\</a\>\</li\>\</ul\>


author: Gnana Sekhar Reddy
body: Hi [@Darryl Lee](/t5/user/viewprofilepage/user-id/1365322)

The above 2 are not working. I am getting all the MOP files/Content links. Can you please looking into it. For your reference i am attaching output also.

1) {{#issue.attachment}}{{#if(exists(filename.match("(MOP.\*\\.docx)")))}}{{filename}}{{\^last}},{{/}}{{/}}{{/}}

2) {{#issue.attachment}}{{#if(exists(filename.match("(MOP.\*\\.docx)")))}}{{content}}{{\^last}},{{/}}{{/}}{{/}}

Even that's not opening format like this below

* [MOP-252_CLONE__Test_1.docx](http://10.168.170.227:8080/secure/attachment/11151/MOP-252_CLONE__Test_1.docx)
* [MOP-252_CLONE__Test_2.docx](http://10.168.170.227:8080/secure/attachment/11152/MOP-252_CLONE__Test_2.docx)
* [MOP-252_CLONE__Test_3.docx](http://10.168.170.227:8080/secure/attachment/11153/MOP-252_CLONE__Test_3.docx)
* [MOP-252_CLONE__Test_4.docx](http://10.168.170.227:8080/secure/attachment/11185/MOP-252_CLONE__Test_4.docx)

Email Out put:

![Screenshot Email.jpg](https://community.atlassian.com/t5/image/serverpage/image-id/234143iC142AF29099D7A31/image-size/large?v=v2&px=999 "Screenshot Email.jpg")

Email Output:

![Screenshot 2023-01-12 180342.jpg](https://community.atlassian.com/t5/image/serverpage/image-id/234144i49516FB3AB118B21/image-size/large?v=v2&px=999 "Screenshot 2023-01-12 180342.jpg")


author: Darryl Lee
body: Hi. I think you may not have fully understood my instructions.

It looks like what you included in the email are supposed to be intermediary variables that are later used within the email.

So you need to use the Create variable action to create the variables I mentioned, like so:

![Screen Shot 2023-01-13 at 1.42.44 AM.png](https://community.atlassian.com/t5/image/serverpage/image-id/234343i51C40DD8A4ED2B45/image-size/large?v=v2&px=999 "Screen Shot 2023-01-13 at 1.42.44 AM.png")

Then you will reference those variables in the email, like so:

![Screen Shot 2023-01-13 at 1.43.07 AM.png](https://community.atlassian.com/t5/image/serverpage/image-id/234344i69FF682CDA7EC201/image-size/large?v=v2&px=999 "Screen Shot 2023-01-13 at 1.43.07 AM.png")

If you have additional issues, please share a screenshot of your rule.


author: Gnana Sekhar Reddy
body: Hi [@Darryl Lee](/t5/user/viewprofilepage/user-id/1365322) Please find attached Screenshot of my Automation rule.

![Screenshot Automation rule.jpg](https://community.atlassian.com/t5/image/serverpage/image-id/234377iB4901FC2AD14E1B4/image-size/large?v=v2&px=999 "Screenshot Automation rule.jpg")


author: Gnana Sekhar Reddy
body: Hi [@Darryl Lee](/t5/user/viewprofilepage/user-id/1365322) for this below screenshot which new action you took for that( Create Variable).

![Screenshot 2023-01-13 172426.jpg](https://community.atlassian.com/t5/image/serverpage/image-id/234381i5D4BEB70F32FA772/image-size/large?v=v2&px=999 "Screenshot 2023-01-13 172426.jpg")

I struck here. Can you please help me out. I am following your steps only.

![Screenshot 2023-01-13 172632.jpg](https://community.atlassian.com/t5/image/serverpage/image-id/234382iEE9F9B248341BA67/image-size/large?v=v2&px=999 "Screenshot 2023-01-13 172632.jpg")


author: Darryl Lee
body: The manual trigger I am using is just for testing.

What you need to do is edit your existing rule and insert the 2 "Create variable" actions after your trigger, and then update the Attachments section of your email to use the code I gave you (that includes the "split" commands).


author: Gnana Sekhar Reddy
body: Hi [@Darryl Lee](/t5/user/viewprofilepage/user-id/1365322) is it possible to attach(Implement smart Values) based on Current Document Version of an Jira issue. For your reference find attached screenshot.

If possible means that could be find so we can easily attach latest version document.

![Screenshot 2023-01-16 210110.jpg](https://community.atlassian.com/t5/image/serverpage/image-id/234639i2A4C6EABAFB83C60/image-size/large?v=v2&px=999 "Screenshot 2023-01-16 210110.jpg")


author: Darryl Lee
body: I don't think there's a way to use a field value as part of a match function, so no, I don't think that will work.

Again, in my testing, I used the **Create variable** action to create two variables, and then I referenced those variables in the email, and this seemed to consistently get the most recent attachment.

The screenshot you shared did not contain those actions, nor do I see the check I suggested to ensure that there is in fact a MOP.\*\\.docx file attached.

Again, I would go back to my previous screenshots and steps that I shared, and make sure you include all the steps. You can't skip any and expect this to work.


author: Gnana Sekhar Reddy
body: Hi [@Darryl Lee](/t5/user/viewprofilepage/user-id/1365322) I am unable to create the Create Variable in Jira Automation. We are using DC Flat form. Is there any option to create that in DC Level. Can you please confirm me.![Screenshot 2023-01-17 145834.jpg](https://community.atlassian.com/t5/image/serverpage/image-id/234753iE44483CDCBCE2AB0/image-size/large?v=v2&px=999 "Screenshot 2023-01-17 145834.jpg")


Tags: data-center

jira