Why am I getting a JS Exception when accessing one of the context variables

Not applicable

I’m getting the following exception when I try to loop through the collection returned by the request.queryparams.names variable using javascript:

"Javascript runtime error: \"Access to Java class \"java.util.HashSet\" is prohibited” Here’s a sample code snippet where the failure point is at line 2.

var queryFieldsCollection = context.getVariable('request.queryparams.names');
for (var k in queryFieldsCollection) {
    print(queryFieldsCollection[k]);
}
How can I access this collection?
Solved Solved
1 1 1,312
1 ACCEPTED SOLUTION

Not applicable

We use compiled JavaScript written in Java, and there's a limitation on accessing Collections. It's not possible to iterate directly through the collection…this is a limitation in the library we use. To get around that, convert the collection to a string, tweak the string, then split it into an array, and loop through that array. Here's a sample.

//Convert Collection to string
var queryFieldsCollection = context.getVariable('request.queryparams.names') + '';

//Remove square brackets
queryFieldsCollection = queryFieldsCollection.substr(1, queryFieldsCollection.length - 2);

//Split string into an array
var queryFieldsArray = queryFieldsCollection.split(", ");

//Loop through Array and get value of queryparam
for (var i = 0; i < queryFieldsArray.length; i++) {
    print( queryFieldsArray[i] + ":" + context.getVariable('request.queryparam.' + queryFieldsArray[i]) );
}

View solution in original post

1 REPLY 1

Not applicable

We use compiled JavaScript written in Java, and there's a limitation on accessing Collections. It's not possible to iterate directly through the collection…this is a limitation in the library we use. To get around that, convert the collection to a string, tweak the string, then split it into an array, and loop through that array. Here's a sample.

//Convert Collection to string
var queryFieldsCollection = context.getVariable('request.queryparams.names') + '';

//Remove square brackets
queryFieldsCollection = queryFieldsCollection.substr(1, queryFieldsCollection.length - 2);

//Split string into an array
var queryFieldsArray = queryFieldsCollection.split(", ");

//Loop through Array and get value of queryparam
for (var i = 0; i < queryFieldsArray.length; i++) {
    print( queryFieldsArray[i] + ":" + context.getVariable('request.queryparam.' + queryFieldsArray[i]) );
}