Why doesn't the diff function work correctly with date values in lists?

Mike.Mikaelian October 11, 2024

I've run into an issue in Jira Automation that looks suspiciously like a bug. I wanted to take the results of a JQL search and total up the number of days between two date values in the results. I first used the Branch Rule widget to search up each issue individually and arrived at a function that properly calculated the number I was looking for. However, the Branching Rule widget cannot pass values back to the main rule, so I was unable to do much of anything useful with this result.

To remedy this, I used the Lookup Issues widget to get the same search results and then used a modified version of the same function (I had to slightly change the smartvalues to remove the "issue." prefix, but otherwise it's the same function, see below). When I did, it seemed incapable of properly calculating the same values. Here's the bug report:

Steps to Reproduce

  1. Create an automation rule you can trigger manually
  2. Add a Lookup Issues action that performs a search for issues that have to date fields used to define a range of time (we'll call them startDate and endDate).
  3. Add a Log action that calculates the difference between the two dates for each issue in the search result:
    {{#lookupIssues}} {{startDate.format("M/d")}}-{{endDate.format("M/d")}} {{startDate.jiraDateTime.toDate.diff(endDate.jiraDateTime.toDate.plusDays(1))}} {{/}}
  4. Run the rule
  5. Look at the Audit Log

Expected Results

The calculated numbers should equal the number of days between (and including) the startDate and endDate for each issue.
Ex: 10/11-10/11 1 day, 10/17-10/23 7 days, 10/7-10/7 1 day, 10/18-10/18 1 day, 10/8-10/8 1 day

Actual Results

After the first result, there is no rhyme or reason to the results except that they alternate between positive and negative values.
Ex: 10/11-10/11 1 day 10/17-10/23 -5 days 10/7-10/7 5 days 10/18-10/18 -6 days 10/8-10/8 4 days

2 answers

1 vote
Bill Sheboy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 15, 2024

Hi @Mike.Mikaelian 

First thing, what problem are you trying to solve?  That is, "why do this?"  Knowing that may help the community to offer better suggestions.

Next, and as a short answer: there appear to be field type errors and missing syntax in that iterator expression.  Please see details below.

 

The startDate and endDate are not built-in field's smart values, so I expect at least one is a custom field added to the project / site.  What are the types of the fields: text, date picker, date / time picker, etc.?

There is a built-in "Start date" field, with a smart value of the same name.  Perhaps use this how-to article to confirm the smart values (or custom field ids) in your expression: https://support.atlassian.com/cloud-automation/docs/find-the-smart-value-for-a-field/

 

The "Start date" field is a date picker field.  If that is the field you wanted to use, why are you formatting that to a text date / time string, and then back to a date / time type before performing the diff() operation?  Why not just use the values directly?

 

Why are you incrementing the "end date" by one day?  Is that in the case the dates are the same so the count is always at least 1 day?

 

Next, your diff() function call is missing the units of measure, and so I would expect the results to be unpredictable.  Please review this information to decide which units you need: https://support.atlassian.com/cloud-automation/docs/jira-smart-values-date-and-time/#Date-difference---

 

Kind regards,
Bill

Mike.Mikaelian October 15, 2024

The reason I'm doing this is to capture the difference between two custom date fields. 

Through trial and error I found that I had to transform these date values specific ways to get them to be recognized as dates for these functions. Due to the needs of my project, the built-in Start Date is not available, and the number of days in the request is not necessarily the value I want (I will be using a conditional to choose between another date and the start or end dates of these issues once I am able to get past this bug).

The additional day is needed to get the number of days in the range, because the diff function subtracts one from the other, giving you a value that's one less than the number of days the range represents. Again, I am using other functions (the bug has a lot of that stripped away since I was able to establish those transformations were not causing the issue). 

Interestingly, I didn't notice that I had left off the units; thank you for calling that out! However, when I included the units in the function, it still provided the same results. I'm going to poke at that some more and make sure I haven't made any other syntax errors.

Bill Sheboy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 15, 2024

Thanks for that information, Mike!

Just to confirm, you are using Jira Cloud and not Server or Data Center, correct?

 

What are the types of the custom fields?  They seem to be date picker fields, otherwise these would not work with the format() function: 

{{startDate.format("M/d")}}-{{endDate.format("M/d")}}

In that case the conversions to / from text should not be needed.

 

There are some leftover / duplicate smart values in Jira Automation.  For example, "Due Date" has several that work!  I wonder if there is an existing, old startDate one for the "Start Date" field.  The way to force this to work with your fields is to use the custom field ids, for example:

{{issue.customfield_12345.diff(issue.customfield_67890.plusDays(1)).days}}

Perhaps try that with a single, example issue (and your custom field ids) to check for differences.

Mike.Mikaelian October 15, 2024

A single issue doesn't produce the error; it only occurs when trying to take a search result and perform the operation on each issue in the result with the single list command. Taking the command out of the list and using the Branching Results widget provides the correct result. The only reason I'm not using that approach is because I need to sum the values after getting them, and those values can't be passed from the Branching Result to the main part of the rule without storing them outside the rule and retrieving them later.

Bill Sheboy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 16, 2024

Just following up on these questions:

What is your Jira Version: Cloud, Server, or Data Center?

Did you confirm the field types are date picker?

Did you try using the custom field id values rather than the smart values for the field names?  For example, with made up ids:

{{#lookupIssues}}
{{customfield_12345.format("M/d")}}-{{customfield_67890.format("M/d")}}
{{customfield_12345.diff(customfield_67890.plusDays(1)).days}}
{{/}}
Mike.Mikaelian October 17, 2024

I'm on Cloud

The fields are from a date picker

I have narrowed down the issue to the diff function. I've removed all of the other variables, to the point where anyone could take my smartvalue and reproduce this in their own instance. I don't believe I'm doing anything here to cause this. However, because I'm on Jira cloud, I can only report the bug through my admin. I didn't want to do that if it turned out I was causing this. 

Bill Sheboy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 17, 2024

Thanks for that information, and I just tried this scenario on a team-managed project and got a similar result, with missing / incorrect values when the difference was negative.  The positive diffs all worked for me, however...

I found an open defect for this symptom: https://jira.atlassian.com/browse/AUTO-334

 

I tried to create a workaround with conversion to milliseconds or epoch time in a list first, and that did not work either.

Like Walter Buggenhout likes this
0 votes
Eduardo
Contributor
October 12, 2024

The issue you're encountering with Jira Automation's diff function on date values in a list is likely related to how the lookupIssues smart value is processed within a list. Specifically, there could be an inconsistency in how Jira handles date arithmetic within loops or iterations involving lookupIssues.

Here’s a breakdown of why this might be happening and possible ways to address it:

Why the issue occurs:

  1. Jira DateTime Conversion in Lists: When using lookupIssues, Jira may not correctly parse or handle date values when used in list processing (loops). This can result in inconsistent behavior when performing date calculations such as diff.

    The inconsistent output you see (alternating positive and negative values) suggests that the way Jira Automation processes these dates inside a list context might not be robust. This could be due to:

    • Issues with time zone conversions.
    • Incorrect handling of date objects when iterating over multiple issues in the lookupIssues block.
    • Inconsistent recognition of the "plusDays(1)" operation within list contexts.
  2. Smart Values and Context Changes: In the Branch Rule approach, the context of the issue is local and confined to the issue being processed at that moment. This allows date manipulations to work correctly. However, in the lookupIssues approach, the context shifts to an entire list, and the diff operation might not correctly handle date values because the logic is being applied at a different context level (list versus individual issue).

Possible Fixes:

1. Calculate the Difference in the Individual Issue Context:

If possible, use Branch Rule again but try to summarize or pass data through alternative means (e.g., storing values in variables or passing data using JSON). This way, you ensure that the diff function runs in the proper context where it works reliably.

2. Workaround for lookupIssues:

Modify the way the dates are handled in the lookupIssues block by isolating each date calculation outside the list processing. You can try something like this:

 

{{#lookupIssues}}
Start: {{startDate.format("M/d")}}, End: {{endDate.format("M/d")}} 
Difference: {{startDate.jiraDate.diff(endDate.jiraDate)}}
{{/}}

This removes the .toDate and .plusDays(1) from the calculation. You may also want to ensure both startDate and endDate are correctly formatted as dates before performing the diff operation. Use .jiraDate instead of .jiraDateTime.toDate as this could help simplify things.

3. Ensure Consistent Date Formats:

Ensure both dates (startDate and endDate) are consistently formatted in the same time zone and date format. You can try explicitly setting both date fields to use the same date format before applying the diff calculation, like this:

 

{{startDate.jiraDate.format("yyyy-MM-dd")}} - {{endDate.jiraDate.format("yyyy-MM-dd")}} 
Days: {{startDate.jiraDate.diff(endDate.jiraDate)}} 

4. Debugging with Log Action:

To further investigate, you could log the raw values for startDate and endDate just before the diff function to verify that the dates are correct and in the expected format:

 

{{#lookupIssues}}
Start Date Raw: {{startDate}}, End Date Raw: {{endDate}} 
{{startDate.jiraDate.diff(endDate.jiraDate)}}
{{/}}

This can help identify if there are any anomalies in the raw date values being processed.

It seems like the diff function isn't handling the list processing context as expected. Modifying the logic to calculate the difference outside the list, simplifying the date conversion (avoiding .toDate and plusDays(1) within the loop), and ensuring both dates are in the same format could help. If the issue persists, it might indeed be a bug, and you could raise it with Atlassian support for further investigation.

Mike.Mikaelian October 14, 2024

I've tried all of the suggested troubleshooting tips on the lookupIssues approach and they all produce the same result. I am curious how I can pass values from the branch operation to the main operation without going outside Jira Automate.

Bill Sheboy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 15, 2024

Hi @Eduardo 

Please stop posting bot-generated content.  For more details, please see the community guidelines: https://community.atlassian.com/t5/custom/page/page-id/rules-of-engagement

Thanks,
Bill

Like # people like this

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
TAGS
AUG Leaders

Atlassian Community Events