I have a question regarding increasing the due date of an issue by adding a comment with a specific phrase.
My goal was when a comment is written and the expression +2w or +2W is used, the due date of the task is moved forward by 2 weeks. Specifically, to the Tuesday two weeks from now.
Initially, I hardcoded it with the following code:
{{now.withNextDayOfWeek("TUE").plusWeeks(2)}}
However, I then had the idea to make it dynamic. In other words, even if +6w or +200w is used, the number between the plus sign (+) and the w should be used for the calculation.
Extracting and storing the value in a variable wasn't a problem. However, using that variable within the code mentioned earlier simply didn't work as expected.
To store the value in a variable, I did the following:
Action: Create Variable
Name: numberOfWeeksToBeMoved
Smart-Value: {{comment.body.match("\+(\d{1,3})[wW]")}}
Next, I used the function mentioned above and simply replaced the 2 with my variable.
{{now.withNextDayOfWeek("TUE").plusWeeks({{numberOfWeeksToBeMoved}})}}
While testing the function, I received the following error:
Error rendering smart-values when executing this rule:
Failed to get value for now.withNextDayOfWeek("TUE").plusWeeks({{numberOfWeeksToBeMoved: {{now.withNextDayOfWeek("TUE").plusWeeks({{numberOfWeeksToBeMoved}})}}
While researching, I came across tips suggesting that the issue might be due to the variable not being a number. I tested this using numberOfWeeksToBeMoved.isNumeric, and it returned true as the result.
Therefore, I couldn't imagine that this was the problem.
Especially, I also tested calling my variable with numberOfWeeksToBeMoved.asNumber. Also without success.
Later, I found another method:
{{#now}}func=plusWeeks({{numberOfWeeksToBeMoved}}){{/}}
This method at least worked in the sense that my variable was used in the addition function, and "Today" was calculated in x weeks.
That in turn proves that my variable is recognized as a number.
Now I wanted to somehow incorporate Tuesday, but I simply didn't get the results I wanted.
Test 1
{{#now}}func=plusWeeks({{numberOfWeeksToBeMoved}}).withNextDayOfWeek("TUE"){{/}}
Result: Today in X weeks, without considering Tuesday.
Test 2
{{#now.withNextDayOfWeek("TUE")}}func=plusWeeks({{numberOfWeeksToBeMoved}}){{/}}
Result: String output "func=plusWeeks(3)"
The number to be calculated was correctly extracted, but the function could not be executed correctly.
No matter how I twisted and tried, nothing worked.
Ultimately, I was able to solve the problem myself, but I wonder if there is a simpler or better way.
My knowledge of Smart-Values is limited, but I couldn't imagine it would be theoretically so complex, especially since there's such a simple function for it.
Maybe I'm just overlooking something. Perhaps there's someone else who has tried and implemented something similar.
Here's my workaround
Trigger: When Issue commented
Condition: If Compare two values
First value: {{comment.body}}
Condition: contains regular expression
Regular expression: \+(\d{1,3})[wW]
Action 1: Create variable
Variable name: currentDuedate
Smart Value: {{duedate}}
Action 2: Create variable
Variable name: numberOfWeeksToBeMoved
Smart Value: {{comment.body.match("\+(\d{1,3})[wW]")}}
Action 3: Create variable
Variable name: tuesdayOfNextWeek
Smart Value: {{now.withNextDayOfWeek("TUE")}}
Action 4: Create variable
Variable name: tuesdayInFewWeeks
Smart Value: {{#tuesdayOfNextWeek.ToDate}}func=plusWeeks({{numberOfWeeksToBeMoved}}){{/}}
Action 5: Create variable
Variable name: newDuedate
Smart Value: {{#tuesdayInFewWeeks.ToDate}}func=minusWeeks(1){{/}}
(The calculation with minus one week is necessary because we are counting from next Tuesday plus x weeks.)
Action 6: Edit issue fields
Field: Due Date
Value: {{newDuedate}}
Action 7: Add value to the audit Log
Field: Due Date
Value: {{newDuedate}}
Have a nice day,
Martin
Hi @Martin -- Welcome to the Atlassian Community!
First thing, well done figuring out a workaround!
As a short answer: your solution works so document how it works and explain it to another team member to help with maintenance / improvements.
Next, automation rule features differ between Jira Cloud, Server, and Data Center. As you observed:
I do note a potential problem in the rule explanation you show, so please check your rule:
And so one improvement to reduce the risk of errors is to first create the variable, and then test if the variable is empty before proceeding. Then there is only one instance of the regular expression in the rule.
Kind regards,
Bill
Hello Bill.
Thank you very much for your response and the helpful information!
It’s challenging to keep track when researching solutions, especially with a mix of Cloud, Server, and Data Center environments, but it’s good to know what to pay attention to!
Of course, it’s unfortunate that there is no asNumber conversion in the Server / Data Center version yet. That means you’re often forced to tinker a bit.
In the end, I was lucky that the workaround worked because the date functions combined with mathematical expressions allowed me to interpret my variable as a number.
I actually tested many different variants. The one with {{somefield.someFunction(someOtherField)}} without {{…}} explicitly for the variable as well. I believe that resulted in NULL / empty value. I would need to test it again on Monday, but otherwise it wouldn’t have led to any result; otherwise, I could have used it ^^ But here, it seems like it interpreted the variable as a string and therefore didn’t output anything.
Thank you very much for your tip regarding parsing as a day. I think you mean specifically this part:
Action 2: Create variable
Variable name: numberOfWeeksToBeMoved
Smart Value: {{comment.body.match("\+(\d{1,3})[dD]")}}
(Info: already adjusted in the original above)
Actually, it was just a copy-and-paste error on my part. I had copied my function for testing purposes and replaced "wW" with "dD," then simply copied from there. Silly mistake when creating a forum post *smile*. However, I have corrected it in the original post; it should of course read as follows:
Action 2: Create variable
Variable name: numberOfWeeksToBeMoved
Smart Value: {{comment.body.match("\+(\d{1,3})[wW]")}}
Actually, with the dD variant, it would never have found anything if a user entered +999wW. The IF statement would have correctly entered, but the variable would have remained empty because it was searching for +[n]dD. In this case, it would make sense to check whether the variable is empty and exit if so.
However, since the function is only triggered by +[n]wW and a match is also made for +[n]wW, I believe this additional check is not necessary.
Ultimately, the prefix (the plus sign) and the suffix (the w or W) can also be replaced with anything else, as long as the regex function is taken into account and adjusted accordingly. It seemed logical to me that if a user wants to postpone a due date by weeks, they would use a plus symbol followed by the number of weeks as a digit, and W or w as a suffix to indicate weeks. The goal is to extract the number between the prefix and suffix, and ultimately, the magic lies in the function where this number is used :)
I will set your response to "Accepted." All the points that were swirling in my mind have been thoroughly addressed in your post, and I’ve gained quite a bit of knowledge.
If someone else is ever looking for a workaround like this with the due date, here it is *smile*
Have a nice day,
Martin
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.