Last Updated: February 25, 2016
·
1.874K
· edm00se

Domino SSJS - Return Vector Helper Function

Classic IBM/Lotus Domino situation: want to get single-or-multiple values from a Field in a Document by the getItemValue() method, but unsure if it's a Vector or a String?

This is based on the XSnippet of convert any value to an array by Mark Leusink.

The main difference is that I've implemented this as Domino-SSJS for returning any object as a java.util.Vector instanced object. My initial, and primary, use case was when pulling value(s) from a potentially multi-value field, if it is single, Domino returns a string object. So, to keep from having to always check if typeof currentDocument.getItemValue("FieldName") returns a java.util.Vector, I created a helper function. I expanded it from just handling Vector and String objects to account for java.util.ArrayList (a preference of mine) and simple Array objects. It's not all-inclusive, but should help fit the needs of many.

Snippet:

function getValueAsVector(obj){
    switch(typeof obj){
        case "java.util.Vector":
            //it's already a Vector, just return it
            return obj;
            break;
        case "java.util.ArrayList":
            //it's an ArrayList, return it as a Vector
            var x:java.util.Vector = new java.util.Vector();
            for(i=0;i<obj.size();i++){
                x.add(obj[i]);
            }
            return x;
            break;
        case "Array":
            //it's an Array prototype, return it as a Vector
            var x:java.util.Vector = new java.util.Vector();
            for(i=0;i<obj.length;i++){
                x.add(obj[i]);
            }
            return x;
            break;
        default:
            //it's most likely a String, return it as a Vector
            var x:java.util.Vector = new java.util.Vector();
            x.add(obj);
            return x;
            break;
    }
}