Calculating ticket age in email while looping thru issues
Asked by Greg Bailey:

Hello,

Using Automation for Jira Im creating an escalation email report to be sent for active issues older than 30 days.

The emailed report needs to include the actual ticket age which Im having difficulty calculating using the diff method. I've tried both methods below using smartvalues and neither of them is resulting in the ticket age being included in the emailed report.

Any ideas what Im doing wrong here?

```
// snippits of email report looping thru issues

{{#issues}}


<tr><td style="border: 1px solid black; padding:5px 10px 5px 5px; ">{{#now}}func=businessDaysBetween({{created}}), format="toDays"{{/}}</td>
</tr>
{{/}}



{{#issues}}

<tr><td style="border: 1px solid black; padding:5px 10px 5px 5px; ">{{now.diff(created).days}}</td></tr>
{{/}}

```

Any help is appreciated.

Greg


Answers:
author: Bill Sheboy
body: Hi [@Greg Bailey](/t5/user/viewprofilepage/user-id/338964)

Did you already figure this one out? If not, have you tried including the issue prefix to your smart values:

```
{{now.diff(issue.created).businessDays}}

{{now.diff(issue.created).days}}
```

Best regards,

Bill

comments: author: Greg Bailey
body: Hi Bill,  
Apologies for the delayed response.  

Unfortunately I have not been able to get this to work as expected. I have tried with and without '.issue' in the function.  

I tried again this morning to confirm neither works and I was able to show that neither format is working.  

Is it possible this is only supported on Jira Cloud and not Jira Server?


author: Bill Sheboy
body: Hi Greg,

Would you please post an image of your complete rule? That may provide more context for what is up.

Regarding server versus cloud, there are definite differences in automation. For example, {{#issues}} only works in server for iterating over the result set from a JQL. Cloud needs to use branches or lookupIssues to do that.

Thanks,

Bill


author: Greg Bailey
body: Hi Bill,

Below is a screen cap of the rule itself, which is pretty basic and working perfectly other than the date diff calculation.

And here is the HTML, note this has my attempt at using the FUNC method and the diff method neither is working, with and without the 'issue.' present. The diff functions are authored to show up next the actual dates in the table. Any help is surely appreciated.  

<br />

<br />

\<br\>  
\<br\>  
\<table style="border: 1px solid black; cellpadding: 5px; "\>  
\<tr style="border: 1px solid black;"\>  
\<th style="border: 1px solid black; padding:5px 10px 5px 5px;"\>Issue\</th\>  
\<th style="border: 1px solid black; padding:5px 10px 5px 5px;"\>Status\</th\>

\<th style="border: 1px solid black; padding:5px 10px 5px 5px;"\>Assignee\</th\>  
\<th style="border: 1px solid black; padding:5px 10px 5px 5px;"\>Last Updated\</th\>  
\<th style="border: 1px solid black; padding:5px 10px 5px 5px;"\>Requested by Date\</th\>  
\<th style="border: 1px solid black; padding:5px 10px 5px 5px;"\>Committment Date\</th\>  
\<th style="border: 1px solid black; padding:5px 10px 5px 5px;"\>Requesting Business Owner\</th\>

<br />

\</tr\>

{{#issues}}  
\<tr style="border: 1px solid black;"\>  
\<td style="border: 1px solid black; padding:5px 10px 5px 5px; "\>\<a href="{{toURL}}"\>{{key}}-{{Summary}} \</a\>\</td\>  
\<td style="border: 1px solid black; padding:5px 10px 5px 5px; "\>{{status.name}}\</td\>  
\<td style="border: 1px solid black; padding:5px 10px 5px 5px; "\>{{assignee.name}}\</td\>  
\<td style="border: 1px solid black; padding:5px 10px 5px 5px; "\>{{updated.format("yyyy-MM-dd")}} ( {{#now}}func=businessDaysBetween({{issue.created}}), format="toDays"}} )\</td\>

\<td style="border: 1px solid black; padding:5px 10px 5px 5px; "\>{{customField_12800.format("yyyy-MM-dd")}}({{now.diff(customField_12800).days}})\</td\>

\<td style="border: 1px solid black; padding:5px 10px 5px 5px; "\>{{customField_12801.format("yyyy-MM-dd")}}\</td\>  
\<td style="border: 1px solid black; padding:5px 10px 5px 5px; "\>{{customField_12802.name}}\</td\>

\</td\>  
\</tr\>  
{{/}}  
\</table\>

![rule.png](https://community.atlassian.com/t5/image/serverpage/image-id/122088i34B357A1A07F1FC7/image-size/large?v=v2&px=999 "rule.png")


author: Bill Sheboy
body: Hi Greg,

Thanks for that information... Let's look at this in pieces:

First, you are using a scheduled trigger followed by a branch on JQL. If you combine those (add the JQL to the scheduled trigger and drop the branch) the rule will only fire when there is data to process. Like in this [example in the documentation](https://confluence.atlassian.com/automation/bulk-handling-of-issues-993924653.html).

Remember you are iterating over {{#issues}} so I am unclear what that {{#now}} does with the Func syntax.

Next, please remove wherever you have the "issue." prefix which is within the {{#issues}} loop, and just reference the fields. For example, replace {{issue.created}} with {{created}}

If that does not help, let's try to progressively add pieces to see when it breaks. You can run the scheduled rule repeatedly to test without waiting for the schedule:

(1) Replace your email body with:

```
{{#issues}} {{key}} -- {{summary}} -- {{created}} {{ /}}
```

(2) If that works, then try:

```
{{#issues}} {{key}} -- {{summary}} -- {{created.diff(now).days}} {{ /}}
```

(3) If that works, then try to wrap the above into HTML format table rows.

(4) Finally add back in your other contents to see what happens.

My hypothesis is that (2) will not work. I submitted a defect for Cloud version that functions like diff() do not work within iterators (like {{issues#}} ) and I was informed Atlassian knows about this. They implied this does work for Server version.

In the diff() doesn't work stand-alone, you have 2 possible work-arounds:

1. Create/save a filter to find the active issues older than 30 days, and then use a subscription to send the email on a schedule. You cannot control the email format on this approach.
2. Add a custom field for TicketAge, create another scheduled rule that once per day refreshes the value, and then use your current rule to report TicketAge. That eliminates the need to do the math inside of the {{#issues}} iterator.

Please let me know what you learn.

Best regards,

Bill


author: Greg Bailey
body: Thanks Bill, I'll give this a try, hopefully today.  
And to be clear this automation rule is working totally as expected. Im only trying to add the calculated days from certain date fields, this is just to avoid confusion when the recipients receive the emailed report since I have to lump different ticket status into one table as im unable to break them up in email due to being on server where its' not possible and it is possible in Cloud.

I'll try your suggestion but I have a feeling we'll get the same result, leaving me with only the option of creating a custom field to show days. A filter subscription is not an option here as it is a management requirement that this report be a more 'formal' email.

I'll report my findings when I have them. ;-)

Thanks again for your kind suggestions,

Greg


author: Greg Bailey
body: Bill,   
I simplified the email as you suggested as well as made the change to include the JQL in the scheduling area. No luck. The date diff is still not calculating when a loop is leveraged.

attached is the updated rule settings and here's the email generation. note I did try sending both as HTML and text, same result - no calculated date diff.

```
{{#issues}} {{key}} -- {{summary}} -- {{created}} --age: {{created.diff(now).days}} 
{{/}}
```

However! I did verify that when a single issue action triggers a rule the mail sent does calculate the date diff. note this is not in a loop, just working with a single issue.

This HTML table row entry in an email, sans issue loop, does result in the date diff function correctly showing the date difference.

```
<tr> <td style="font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;border-color:black; font-weight:bold;background-color:#c0c0c0;border-color:#000000;text-align:left"><b>Created and age</b></td> 
<td style="font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;border-color:black; text-align:left">{{created.format("yyyy-MM-dd")}} - {{created.diff(now).days}}</td> 
</tr>
```

So it appears this does not work when included in an issue loop, but does work when working with a single issue.

Again, we are running Jira Server 8.5.3 and currently are on the latest automation for jira version - 7.2.3.


author: Bill Sheboy
body: Hi [@Greg Bailey](/t5/user/viewprofilepage/user-id/338964)

I think this is one of those "functions as designed" situations, where the functions/operators do not work within an iterator. To confirm, I suggest submitting a separate defect for the Server version to confirm with Atlassian.

To get what you originally requested, I believe the work-around I gave will work: add a custom field (TicketAge), set it with another daily scheduled rule, and then report on the custom field. This approach will work because each issue is processed individually, and so the iterator is just reporting the custom field contents.

Best regards,

Bill


Tags: server

smart-value