Automation: ADD "earliestUnreleasedVersion()" to fixVersion
Asked by jeff.kehler:

I am trying to do what I thought would be simple enough but I have hit a wall.

When an issue is transitioned (got this),

I want to ++**ADD**++the earliestUnreleasedVersion() to the fixVersion field. All I can do at present is REPLACE the value.


Answers:
author: Jack Brickey
body: Hi [@jeff.kehler](/t5/user/viewprofilepage/user-id/3708395) , for adding to existing field values you need to use advanced edit JSON. Is that what you are doing? Please see this article that illustrates this for the labels field. You can easily adapt to fixVersions. [advanced-field-editing-using-json](https://support.atlassian.com/cloud-automation/docs/advanced-field-editing-using-json/#Fix-Versions)

comments: author: Bill Sheboy
body: Hi [@jeff.kehler](/t5/user/viewprofilepage/user-id/3708395)

*Yes, and*...to Jack's suggestions...

Once you have learned about making the updates with JSON to "add" a Fix Version, there are several ways to identify the earliest unreleased version to add it:

* assuming there are other issues already assigned to that version, you could use Lookup Issues to find an example issue and get the value
* use the Send Web Request action and the REST API to get the list of versions and identify the correct one
* brute force:
  * save any Fix Versions in the trigger issue in a created variable
  * update the issue, setting the value to the earliest unreleased one
  * re-fetch the issue
  * now perform a JSON edit, combining the prior and current values in the field

Kind regards,  
Bill


author: jeff.kehler
body: [@Jack Brickey](/t5/user/viewprofilepage/user-id/853369) [@Bill Sheboy](/t5/user/viewprofilepage/user-id/1685313) All I am trying to do is ADD the "Next release by release date" to any existing values already present. I used the brute force method above.

I have the right values captured in variables. I get exactly what I want for the fixVersion field in my audit log. I just cannot find the path to get the values in the fixVersion field.

***Capture existing values in fixVersion in new Variable***

1. {{varCurrentFixVersions}}
   1. issue.fixVersions.name
2. Log action - {{varCurrentFixVersions}}
   1. CORRECT RESULTS: May 20 Bug Fix, May 13 Bug Fix

***Set fix Version to Next unreleased by release date.** ++Fix version is selected++*

![2024-07-18 13_15_36-Audit log - Automation - Guardian Admin Suite - Jira.png](https://community.atlassian.com/t5/image/serverpage/image-id/336440i78E031B485C8DE27/image-size/large?v=v2&px=999 "2024-07-18 13_15_36-Audit log - Automation - Guardian Admin Suite - Jira.png")

***Here I had to add a 5x re-fetch delay** allowing time for logic to execute and populate fixVersion field.*

***Capture "new" existing value in fixVersion in new Variable***

1. {{varNextRelease}}
   1. {{issue.fixVersions.name}}
2. Log action - {{varNextRelease}}
   1. CORRECT RESULTS: July 23 Bug Fix

**From here I have tried everything I could find but nothing worked**

Simply adding both variables in the EDIT issue fields as smart values.

i.e. {{varNextRelease}} , {{varCurrentFixVersions}} **NO GO**But selecting 2 other named releases/versions works fine

*** ** * ** ***

I have tried custom JSON **NO GO**But no errors in audit log AND both variables have exactly what I want. This just clears the fixVersion field; whether I use ADD or SET, makes no difference.

If I reverse the field order, I end up with just the varNextRelease

{  
"fields": {  
"fixVersions": \[  
{  
"add": {  
"name": "{{{varCurrentFixVersions}},{{varNextRelease}}"  
}  
}  
\]  
}  
}

*** ** * ** ***

I have gone to some AI and all I got was the same things I found, none worked.

***I find it hard to believe that something so simple, as appending rather then replacing could be this difficult. Manually it is simple. Doing bulk updates, it is simple.***

I have to just be missing something ... simple.


author: Bill Sheboy
body: First thing: if the version update with Edit Issue is taking so long it requires multiple Re-fetch Issue actions to succeed, you may want to use another method (i.e., the REST API call). One of those re-fetches should have been enough for this type of update.

Moving on with what you have started already...

In this case, you are trying to *set the field* and not use "add" with JSON. Thus the syntax will be this, as Jack first described:

```
{
  "fields": {
    "fixVersions": [
      { "name": "version1" }, 
      { "name": "version2" }
    ]
  }
}
```

Thus the key is how to merge those entries in the variables...

First, when saving the current versions, I recommend taking complete control of the delimiters to avoid surprises:

* create variable
  * name: varCurrentFixVersions
  * smart value: {{issue.fixVersions.name.join(",")}}

Now concatenate on the new one with this:

```
{{varCurrentFixVersions.concat(",").concat(varNextRelease)}}
```

That is now a comma-separated values list of version names. You may want to consider a condition check in case the field had no existing version values.

Finally, that expression can be split() into a list to dynamically generate the JSON for the update:

```
{
  "fields": {
    "fixVersions": [
      {{#varCurrentFixVersions.concat(",").concat(varNextRelease).split(",").distinct}}
        { "name": "{{.}}" } {{^last}}, {{/}}
      {{/}}
    ]
  }
}
```

That {{\^last}}, {{/}} part conditionally adds the trailing comma so the JSON parsing does not fail.


author: Jack Brickey
body: [@Bill Sheboy](/t5/user/viewprofilepage/user-id/1685313) , excellent write up as always. Thanks for your expertise!


author: jeff.kehler
body: [@Bill Sheboy](/t5/user/viewprofilepage/user-id/1685313) Thanks for the response.

I am still not able to get it working as needed. Not sure I am following.

* The variable **varCurrentFixVersions** is: {{issue.fixVersions.name.join(",")}}
  * **Log Result** : \[GLIC Aug 20 Bug Fix\]
* The set fixVersion using EDIT to Next unreleased version by release date
* The variable **varNextRelease** is: {{issue.fixVersions.name}}
  * **Log Result**: GLIC July 23 Bug Fix
* Advanced edit:
  *

    ```
    {
      "fields": {
        "fixVersions": [
          {{#varCurrentFixVersions.concat(",").concat(varNextRelease).split(",")}}
            { "name": "{{.}}" } {{^last}}, {{/}}
          {{/}}
        ]
      }
    }
    ```

  * **Log Result**: Error while parsing additional fields. Not valid JSON.

**The end result in the issue itself** is still, just replacement of the original value with the latest.

I cannot see why the first variable has the braces, but I am guessing that is causing the issue?

I am also not clear where I am to do the following.

Now concatenate on the new one with this:

```
{{varCurrentFixVersions.concat(",").concat(varNextRelease)}}
```

I am not seeing in your outline, the step where I actually set and capture the varNextRelease, so I kept those steps "as is" and assumed the concatenation was handled in the advanced edit step???


author: Bill Sheboy
body: Sorry, I should have tested that earlier post rather than just typing it in.

~~It appears **joining the fix versions adds square brackets for array notation** , so I added a substringBetween("\[", "\]") call to remove them. Please see the adjustment I made to capture varCurrentFixVersions**.**~~

*Update: the above statement handled the typo in the first variable, creating nested lists with square brackets. After that was corrected the substringBetween() call could be removed.*

With that change, it should all work. And so the overall rule flow is this:

* trigger: *some trigger with your issue*
* action: create variable for varCurrentFixVersions
* action: edit issue to update the Fix Version to the earliest unreleased
* action: re-fetch issue
* action: create variable for varNextRelease
* action: edit issue, with the dynamic JSON expression to combine the values

If that does not work, please write each created variable and the JSON expression to the audit log and post what you see.

Tip: you could create another variable to build the JSON, perhaps named varJson, so it can be logged and then used in the edit as {{varJson}}


author: jeff.kehler
body: [@Bill Sheboy](/t5/user/viewprofilepage/user-id/1685313) Thank you for all your help.

There were a few errors needing correction. Above we had "issue++s++.fixVersion.name" which should be "issue.fixVersion.name".

I also did not end up needing the substringBetween("\[", "\]") call addition. I removed the space after the comma in the JSON, and that seemed to fix things.

***In any case, with some trial and error, this is what I have, now working. Still seems like A LOT of extra work to just "add" an item to an array versus "replacing". :-)***

Tested \& Passed in all scenarios.

1. Add Next with NO existing fixVersion
2. Add Next with One (1) existing fixVersion
3. Add Next with More than 1 existing fixVersions
4. Current fixVersion already present
5. Current fixVersion already present +1
6. Current fixVersion already present + more than 1

**RULE SET UP**

* Create**First** variable **varCurrentFixVersions**
  * {{issue.fixVersions.name.join(",")}}
  * Add value to audit log
    * varCurrentFixVersions: {{varCurrentFixVersions}}
* Edit issue fields:
  * fixVersion selected
  * SET "Next unreleased version by release date"

![2024-07-22 05_59_12-Audit log - Automation - Guardian Admin Suite - Jira.png](https://community.atlassian.com/t5/image/serverpage/image-id/336972iAE6AA6871F1E9A86/image-size/large?v=v2&px=999 "2024-07-22 05_59_12-Audit log - Automation - Guardian Admin Suite - Jira.png")

* Re-fetch issue data
* Create **Second** Variable **varNextRelease**
  * {{issue.fixVersions.name}}
  * Add value to audit log
    * varNextRelease: {{varNextRelease}}
* Create **Third** variable **varCorrectFixVersions**
  * { "fields": { "fixVersions": \[ {{#varCurrentFixVersions.concat(",").concat(varNextRelease).split(",")}} { "name": "{{.}}" } {{\^last}},{{/}} {{/}} \] } }
  * Add value to audit log
    * varCorrectFixVersions: {{varCorrectFixVersions}}
* **EDIT ISSUE FIELDS - More options**
  * { "fields": { "fixVersions": \[ {{#varCurrentFixVersions.concat(",").concat(varNextRelease).split(",")}} { "name": "{{.}}" } {{\^last}},{{/}} {{/}} \] } }

**KEY NOTE FOR OTHER NOOBS LIKE ME**: Ensure no field is selected in EDIT drop down when using advanced/JSON


author: Bill Sheboy
body: Well done!

For your test cases 4-6, and to avoid errors from attempts to add duplicate versions in the JSON, you could add .distinct after the split(",") as that will remove the duplicates.

Regarding the typo {{issues... versus {{issue... **thanks for seeing that!** It was a coincidence it worked as the smart value {{issues}} (plural) is a feature of bulk-issue handling of triggers and branches for Jira Server and Data Center automation. (Jira Cloud uses Lookup Issues instead.) I recall testing it several years ago and found it inconsistently worked with Cloud; IMO it probably should throw an error as it is unsupported.

And so, that is what caused the difference with the square brackets: the plural smart value detected possible nested lists (i.e., version lists within an issue list) and so added brackets around the nested list.

I have updated my earlier post accordingly.


Tags: cloud

jira-cloud