How to use a variable in a date calculation for Data Center Automation?
Asked by Jeremy Jedlicka:

I am trying to create a rule that automatically creates a set of sub-tasks when a story is created. Each Sub-task will have Target start and end dates created within the rule based off the target start and end dates of the story. For example if the story has a date difference of 90 days between the start and end dates the first sub-task will have dates that cover 10% of that, or 9 days.

I have a variable which calculates that 10%:

    endDate

    {{#=}}ROUND({{triggerIssue.Target start.toDate("yyyy-MM-dd").diff(triggerIssue.Target end.toDate("yyyy-MM-dd")).days*.1,0){{/}}

When I try to use this variable within a date calculation it doesn't work. When adding it to the audit log, for instance:`{{now.plusDays(endDate)}}`the log is empty. `{{now.plusDays(endDate.asNumber)}}` also doesn't work.

Does anyone know how to use a variable within a date calculation, or at the very least how to get a variable to function as a number?


Answers:
author: Jeremy Jedlicka
body: If anyone searches this in the future, I reached out to Atlassian for this, and they responded that the new syntax Automation uses cannot do this. The old syntax can though. Here is the JSON snippet Atlassian gave me to solve my request

```
{"fields": { 
"Target Start": "{{triggerIssue.Target Start}}",
"Target End": "{{#triggerIssue.Target Start.toDate}}func=plusBusinessDays({{endDate}}){{/}}"
 } 
}
```

JSON

comments: author: Bill Sheboy
body: Thanks for that information, Jeremy.

Did the support team indicate if this was a behavior of Jira Data Center automation rules or something else?

And what did they mean by "new syntax Automation"?


author: Jeremy Jedlicka
body: The syntax automation uses for Smart values apparently changed when Atlassian acquired Automation. I asked for documentation of the old syntax, but they said they didn't have any publicly available.


author: Bill Sheboy
body: Yup...they took down / removed public access to the old Codebarrel blog, documentation, and backlog some time after the Atlassian documentation pages went live.


author: Bill Sheboy
body: Hi [@Jeremy Jedlicka](/t5/user/viewprofilepage/user-id/2189455)

Your expression to perform the date difference is missing the closing double-curly brackets }}

    {{#=}}ROUND({{triggerIssue.Target start.toDate("yyyy-MM-dd").diff(triggerIssue.Target end.toDate("yyyy-MM-dd")).days}}*0.1,0){{/}}

When writing expressions like this, you may want to create them in a text editor which highlights matching brackets, parentheses, etc. in order to see them more easily. Then copy-and-paste them into the rule editor.

Next, created variables are text, and to use them as numbers you may add the asNumber conversion:

```
{{varMyVariable.asNumber}}
```

Kind regards,  
Bill

comments: author: Jeremy Jedlicka
body: My variable does have the closing }}. that was a transcription error.

The {{endDate.asNumber}} produces a null value instead of number as it should based off the documentation here:

[Automation smart values - text fields \| Cloud automation Cloud \| Atlassian Support](https://support.atlassian.com/cloud-automation/docs/jira-smart-values-text-fields/)

it seems trying to convert a variable to a number isn't possible.

Even changing the variable to just be the number 9 and trying to add

```
{{now.plusDays({{endDate.asNumber}})}}
```

results in the error: Failed to get value.


author: Bill Sheboy
body: When inside a set of double-curly brackets, another set is not needed. Please try this:

```
{{now.plusDays(endDate.asNumber)}}
```

If that also does not help...

Please post an image of your complete automation rule and an image of the audit log details showing your rule execution. Those will provide context and may help explain what you are observing.

And, you appear to be using Jira Data Center. I recommend writing the following to the audit log to confirm the smart values contain what you expect:

```
Target start       : {{triggerIssue.Target start}}

Target start toDate: {{triggerIssue.Target start.toDate("yyyy-MM-dd")}}

Target end         : {{triggerIssue.Target end}}

Target end   toDate: {{triggerIssue.Target end.toDate("yyyy-MM-dd")}}

diff value         : {{triggerIssue.Target start.toDate("yyyy-MM-dd").diff(triggerIssue.Target end.toDate("yyyy-MM-dd")).days}}

```


author: Jeremy Jedlicka
body: the suggestion:

```
{{now.plusDays(endDate.asNumber)}}
```

also produces a null value.

What's odd is that:

```
{{#if(endDate.asNumber.isEmpty)}}EMPTY{{/}} {{#if(not(endDate.asNumber.isEmpty))}}NOT EMPTY{{/}}
```

Returns NOT EMPTY, so there is something coming though - it's just not being shown in the audit log and it doesn't work in a date calculation.

I can't provide screenshots because our Jira is on a separate classified network not connected to the internt.


Tags: automation

data-center

jira-data-center