How to pull out the last added value from the fixVersion field?
Asked by Elena:

Hi,  
We need to develop a sil script where the last added version in issue is needed. What are the ways to pull the last added value from the fixVersion field?


Answers:
author: Thiago Masutti
body: Hi Ellen.

There's nothing on the stored data that will tell when a version was added, so I don't know if there's a reliable way to tell which version was recently added.

Considering how DB works we may assume larger ID values are more recent (I wouldn't call that bulletproof, but it may work on most cases).

Then you may choose to work with admGetProjectVersions and admGetProjectVersion to get the version with the higher ID (first field from admGetProjectVersion).

![versions.png](https://community.atlassian.com/t5/image/serverpage/image-id/195595iFBC162640556048A/image-dimensions/473x207?v=v2 "versions.png")

In this example I would assume 1.5 is the more recent version based on the ID (10101).

<https://confluence.cprime.io/display/SIL/admGetProjectVersion>

<https://confluence.cprime.io/display/SIL/admGetProjectVersions>

I hope that helps.

Kind regards,  
Thiago Masutti

comments: author: Elena
body: Hi Thiago.  
To clarify, we need to select the last value from the field in the task. As I understand it, your example is pulling a value from a project. Perhaps there is some way to pull the value out of the task?


author: Thiago Masutti
body: Hi Ellen.

If you need to check the latest value assigned to a Jira Issue, then you may use [getFieldChanges](https://confluence.cprime.io/display/SIL/getFieldChanges) .

The below will get all the changes applied to the field.

```
getFieldChanges("SCRUM-14", "Fix Version")
```

![jira-sil-versions.png](https://community.atlassian.com/t5/image/serverpage/image-id/196852i00B0F6DA55997650/image-dimensions/405x109?v=v2 "jira-sil-versions.png")

The below script will get the latest value for the same field.

```
JFieldChange[] changes = getFieldChanges("SCRUM-14", "Fix Version");

JFieldChange latestChange = changes[0];
date newest = latestChange.changeDate;

for (JFieldChange change in changes) {
  if (change.changeDate > newest) {
    latestChange = change;
  }
}

return latestChange;
```

![jira-sil-versions-2.png](https://community.atlassian.com/t5/image/serverpage/image-id/196853i7ECD4B26C3A31572/image-size/large?v=v2&px=999 "jira-sil-versions-2.png")

Kind regards,  
Thiago Masutti


author: Elena
body: Thanks, it worked!  
How to get version name? For further actions with the script, we need to pull out the version name. For example, set the variable:  
string lastVersion;  
and assign the resulting value to it, in your example it is "Version 3.0"


author: Thiago Masutti
body: Hi Ellen.

That returns a JFieldChange object. The structure is detailed in <https://confluence.cprime.io/display/SIL/Predefined+structure+types#Predefinedstructuretypes-JFieldChange>

You may use one of the following depending on what you need.

```
latestChange.newVal

latestChange.newValString
```

![jira-sil-versions-3.png](https://community.atlassian.com/t5/image/serverpage/image-id/196859iC9747DB2F2542DB1/image-size/large?v=v2&px=999 "jira-sil-versions-3.png")

Kind regards,  
Thiago Masutti


author: Elena
body: Hi Thiago.  
It worked. Thank you.  
I noticed that if, when editing a field, the old value is deleted, changed to another value and then saved, then the log is empty. Is there a way around this to always get the latest value?


Tags: confluence-server

fix-version

power-scripts

server