Correct way to store date difference to a custom field using automation for Jira.
Platform Notice: Cloud Only - This article only applies to Atlassian apps on the cloud platform.
Summary
When working with Jira automation, storing the difference in dates between a custom field and the due date in a new custom field requires a smart value. The formula for this operation is {{issue.customfield_10189.diff(issue.duedate).prettyPrint}}. However, caution must be exercised when choosing the type of the new custom field, as certain types can lead to errors.
Diagnosis
In Jira automation, it is common to want to calculate and store the difference in dates between a custom field and the due date of an issue. The smart value mentioned above provides an elegant solution for obtaining this date difference. However, when trying to store this result in a new custom field, the field type becomes crucial.
Cause
Choosing an inappropriate field type for the custom field where the date difference is to be stored can result in errors. If a field of type 'Number' or 'Date Picker' is selected, Jira may encounter difficulties converting the result, which is a combination of both a number and text.For example, if you store a negative result (such as -4) in a Number field, the automation flow fails with this error: "Could not convert the field value to a number. Please ensure the value is a number, math expression, or smart-value that can be converted into a number
Solution
To successfully store the date difference, follow these steps:
Create a Text Custom Field:
Navigate to Jira Settings > Issues > Custom fields.
Click on "Add Custom Field" and select "Text Field" as the type.
Configure Automation Rule:
Set up an automation rule that triggers when needed.
Use the smart value
{{issue.customfield_xxxxx.diff(issue.duedate).prettyPrint}}within the rule to calculate the date difference.Replacecustomfield_xxxxxwith the ID of your own date custom field.duedaterefers to the work item's Due date field.
Store Result in Text Field:
In the automation rule, add an action to set the value of the newly created text custom field with the smart value result.
By choosing a 'Text' field type for the custom field, you ensure compatibility with the mixed-format result ("20 days") and prevent conversion errors.After the automation flow runs, open an affected work item and check that the Text custom field shows the date difference (for example, "20 days"). If the field is blank, check the flow's audit log for errors.
Screenshot of Automation Rule:

Was this helpful?