vRealize Automation Advanced forms and Sorting Properties

February 10, 2022 1 By Allan Kjaer

If you are using properties to use a key/value pair, this are not sorted, so they may show up randomly, and that is not pretty.

In this example I create a properties variable and puts in some countries and there short name, so I want to show the country’s full name and get the short name.

I have created a vRealize Orchestrator action for this, that returns “Properties”, and this is just a simple script.

Return Properties
var countries = new Properties();
countries.put("DK","Denmark");
countries.put("DE","Germany");
countries.put("UK","United Kingdom");
countries.put("NO","Norway");
countries.put("SE","Sweden");
return countries;

The result in the dropdown looks like this.

Unsorted Countries

So to fix this we have to get the properties sorted, but there are no function to do that, and properties can’t be sorted, but vRealize Forms dropdown, can also use a Array of Properties, and with that we can get this list sorted.

Here is the modifies action that now returns a “Array of Properties”.

Properties Array
var countries = new Properties();
countries.put("DK","Denmark");
countries.put("DE","Germany");
countries.put("UK","United Kingdom");
countries.put("NO","Norway");
countries.put("SE","Sweden");

var tempDataArray = new Array();
for each (key in countries.keys) {
    tempDataArray.push(countries.get(key) + "@@" + key);
}

var output = new Array();
for each (item in tempDataArray.sort()) {
     var prop = new Properties();
     prop.put("label",item.split("@@")[0] + " (" + item.split("@@")[1] + ")");
     prop.put('value',item.split("@@")[1]);
     output.push(prop);
}

return output;

So the script takes the set of properties and converts them into a single sting with “@@” as a seperator for key and value, and puts it into a temporary Array, (depending on if you want to sort on the key or value, you may need to switch them around). After this we just loop thru this Array (sorted), and creates a new set of properties, but now they need to be split into two properties “label” and “value”, and the values of this must be the a split of the string for the temporary Array.

The sorted dropdown looks like this, I have added the country’s shotname, for bette visibility.

Sorted Countries with add short name

Hope this will help you.

Some the script comes from the community thread: https://communities.vmware.com/t5/vRealize-Automation-Tools/vRA-8-Sorting-of-properties-for-dynamic-list-in-custom-form/td-p/2285262

Please share this page if you find it usefull: