The CreatorCon Call for Content is officially open! Get started here.

Getting and iterating through Packages.java.util.Properties() object

Mike Moody
Kilo Guru

I'm trying to iterate through the contents of a Packages.java.util.Properties() object.

According to this site the java object in question has a .propertyNames() function which is supposed to return an Enumeration, I'm not sure how to code this in javascript in Service-Now. An example of how to use it is provided here for Java, but I need to get the enumeration in javascript and iterate through it: http://www.tutorialspoint.com/java/util/properties_propertynames.htm

Here is what I've tried:
var props = new Packages.java.util.Properties();
props.put('f_assettag_value', f_asset_tag);
props.put('f_serialnum_value', f_serial_number);
props.put('f_hostname_value', f_hostname);

var allSetProperties = new Enumerator(props.propertyNames());
allSetProperties.moveFirst();
while (allSetProperties.atEnd() == false) {
gs.log('Props key: ' + allSetProperties.item().toString());
}

I am getting this error:
org.mozilla.javascript.EcmaError: "Enumerator" is not defined.
Caused by error in Business Rule: 'Get Asset Details - Failed' at line 171

168: props.put('f_serialnum_value', f_serial_number);
169: props.put('f_hostname_value', f_hostname);
170:
==> 171: var allSetProperties = new Enumerator(props.propertyNames());
172: allSetProperties.moveFirst();
173: while (allSetProperties.atEnd() == false) {
174: gs.log('Props key: ' + allSetProperties.item().toString());

I'm not really sure how to return the Java object's Enumerator to a Javascript Enumerator variable that I can run through.

Any help would be appreciated. Thanks!

1 REPLY 1

Mike Moody
Kilo Guru

Looks like I had the wrong approach. Working code:

var props = new Packages.java.util.Properties();
props.put('f_assettag_value', f_asset_tag);
props.put('f_serialnum_value', f_serial_number);
props.put('f_hostname_value', f_hostname);

var allSetProperties = props.propertyNames();
while (allSetProperties.hasMoreElements() == true) {
gs.log('Props key: ' + allSetProperties.nextElement(), 'key');
}