How to iterate on variables in a Script task?

Hi,
In a javascript Script task, I would like to iterate over the variables. I am trying this:

var vars = execution.getVariables()
print("vars="+vars) // OK works fine
var keys = vars.keySet()
print("keys"+keys) // OK: keys[a, b] (however, I don't understand why it's not ["a", "b"] with quotes)
for (var i = 0; i < keys.length; i++) {
  var k = keys[i]
  print("key="+k) // prints key=undefined
  var v = vars.get(k)
  print("v="+v)
  print(i + " type = " + v.type)
}

This breaks when trying to get the key, i.e. the variable names: I get undefined instead.
Would you please know how to fix this?
I have also tried to find the “execution” API documentation. I thought I would find it in the Java API section, but could not. Can you please tell me where it is?
It is also unclear to me how it is exposed to JavaScript btw.

Many thanks for any advice!

Thanks to VariableScope (Camunda Platform Javadocs 7.17.0-alpha1-ee)

I could fix it by adding toArray():

var keys = vars.keySet().toArray()

2 Likes