3
votes

I have a scoped variable that stores an archive:

viewScope.MY_SCOPE = new Array();
viewScope.MY_SCOPE.push(["id0", 0, true]);
viewScope.MY_SCOPE.push(["id1", 1, false]);
viewScope.MY_SCOPE.push(["id2", 3, true]);

now I want to update one of item.

viewScope.MY_SCOPE[1][2] = "true";

and this fails with the error:

Error while executing JavaScript action expression put(int index,FBSValue value) not supported in JavaWrapperObject.

How do I update a specific item in the array?

2
in the above code, viewScope.MY_SCOPE[1][2] returns false. Your error suggests that you are misusing your library in some way. Can you show us more relevant code? - Patrick Gunderson
@forgivenson you're right and I edited - Patrick Gunderson
this is it.. nothing more specific.. I just need to update one of array item.. Is there specific way to use .PUT for arrays to update items? - VladP
In pure javascript, that assignation is fine. But since you have a similar error than the one in this thread, I assume it has something to do with XPages and the way you use it. - Volune
The problem is likely that the array is stored in viewScope which is a Java HashMap. So there issue related to how you're going through the hash map to get to the array. Michael's answer solves that by first getting the array and putting it into a variable. Then you work with the local object/array as expected, and then you put it back into scope for storage. - David Leedy

2 Answers

2
votes

When adding the SSJS array object to the scope, it is converted to a java.util.Vector. Hence, if you want to set the value, you should use

viewScope.MY_SCOPE[1].set(2,"true");

instead of viewScope.MY_SCOPE[1][2] = "true";.

I think the problem is that using ...[2] = "true" tries to execute the put method of the given object. While put is available in Maps like HashMaps or the scope maps, Vectors use set instead of put to change values. For that reason, you get the "action expression put(...) not supported" error. In contrast to this, it's no problem to get the variable with viewScope.MY_SCOPE[1][2] because the get method is availiable in both HashMaps and Vectors.

1
votes

When storing arrays in scope variables, I like to put the value into a properly typed javascript variable, make edits, and then replace the scope variable with the updated value.

In your case I would do the following:

var tempVar = viewScope.MY_SCOPE.toArray();  //convert to array to make sure properly typed
tempVar[1][2] = true;
viewScope.put(MY_SCOPE,tempVar);

Update: After testing your code along with mine, I too get the same error. To be honest, I never would have messed with multi dimensional arrays in the first place. This is a perfect opportunity to use an array of objects:

var tempVar = [];  // initialize the array

tempVar.push({val1:"id0",val2:0,val3:true});
tempVar.push({val1:"id1",val2:1,val3:false});
tempVar.push({val1:"id2",val2:3,val3:true});
viewScope.put("MY_SCOPE",tempVar);

Then to change the desired value:

var tempVar = [];
tempVar = viewScope.get("MY_SCOPE");
tempVar[1].val3 = true;
viewScope.put("MY_SCOPE",tempVar)

I tested this method and it works fine.