The goal of this article is to showcase how to do IF/Else branching with variables, and then use said variables in JSON for advanced editing.
The problem:
I got a ticket recently to setup an automation rule that would allow one work item to edit another linked work item. Pretty simple, shouldn't take too long. But as is the case with all work tickets, scope creep happened, and with it, the opportunity to learn something new!
The standout feature of this automation rule was adding a comment to the linked work item if either the summary of the trigger work item changes, or a specific custom field changes. Along with copying all the other fields as well.
Seeing that the Edit Issue Fields action allows for advanced editing via JSON, I decided to use that instead of doing the normal thing that any other human being would do and use the "add comment" action. After all, where's the fun in that?
The journey:
Scouring the Atlassian forums and the articles on the Atlassian site for smart values, I couldn't find anything specifically calling out how to do IF/Else branching inside a smart value. I did find the syntax for If conditions being as follows:
{{#IF(True/False condition)}}
Do stuff
{{/}}
I also found syntax for IF/Else in the following manner:
{{IF(True/False condition, value if True, value if False)}}
Those two approaches worked for the most part, but failed when I tried to do something in the "else" block (the first approach did not allow for Else blocks at all, or none that worked reliably that I could find).
What DID work however, is forsaking all that, and creating a {{JSON}} variable within a conditional branch before my Linked Work Items branch.
The solve:
A simple IF/Else conditional branch before my Linked Work Items branch (that would have the Edit fields action and would ultimately have the {{JSON}} variable in the Additional Fields section) that would create a variable based on what's changing.
The logic in the IF/Else condition would look like so:
- if {{changelog.summary.tostring}} != Empty
- Create variable —> {{JSONText}} —> The Summary on the trigger issue has changed, please check to see if the change should be reflected in this issue, the new value is: {{changelog.summary.tostring.jsonEncode}}.
- Create variable —> {{JSON}} —> {"update": {"comment": [{"add": {"body": "{{JSONText}}"}}]}}
- More conditions to set the {{JSON}} for different scenarios
And similarly for the other custom field. The most important part here for showing the new value in a comment is the .jsonEncode, this is VERY important as it switches out the newlines to \n, and allows the JSON to go through correctly without throwing the "Invalid JSON" error I spent the better part of today trying to figure out.
And that's it! Don't be like me, don't try to figure this out through smart value conditional logic. The regular IF/Else branching works way better. Better yet, just use the add comment action block! But if you choose not to, here you go.
One last note, the changelog is accessible for custom fields, you just need the customfield name instead of the id. It'd be something like {{changelog.This Is A Custom Field.tostring}} (yep, with the spaces and all, no " ).