A Legacy Notes Developer's journey into madness.

Concatenation Baby!

Devin Olson  August 10 2012 08:00:00 AM
I spent quite a bit of time today trying to figure out why my SSJS wasn't working the way I intended.  

I created a function that was supposed to return the UniversalID of a document that had a particular RecordID.  Once documents of this particular type are created their RecordID never changes -A perfect candidate for applicationScope.   (If you don't know what applicationScope is don't worry -I'll post about that later on).

So I did a check for that in my function.  The problem is that my function simply refused to work.

Here is the code:
01. function
getUNIDforQueueID(queueID:String):String {
02. /*
03. getUNIDforQueueID
04. Gets the UniversalID for a specified queueID
06. @param queueID: RecordID for which to get the UniversalID
07. @return: UniversalID for the queue document.
08. */        
09. if
(@IsBlank(queueID)) { return ""; }
10. var
tag:String = "unid.recordID." & queueID;
11. if
(applicationScope.containsKey(tag)) { return applicationScope.get(tag); }
13. var
result:String = @Trim(@DbLookup(@DbName(), "lkp-QueuesByRecordID", queueID, 1, "[FailSilent]")); applicationScope.put(tag, result);                
14. return
15. } // getUNIDforQueueID

If you have been writing SSJS or just plain old Javascript and have been away from LotusScript / VBA for very long you probably figured out my problem immediately.   I however have been thinking in LotusScript for many years now, and old optimization habits not only die hard, they are ingrained as muscle-memory when typing code.  

In LotusScript, the character for string concatenation is the ampersand (&).  The plus character (+) is overloaded to conditionally perform concatenation.   What this means is that most LotusScript developers use the plus symbol for concatenation, but a very select few of us use the ampersand.  

In Javascript, the & character is a BITWISE AND , and has nothing to do with concatenation.  (Bitwise AND / OR / NOT / XOR code processing is really cool and allows you to do some super neato things).  In the above code I though I was concatenating the strings "unid.recordID." and whatever the value of the passed parameter was.  What I was really doing was freaking out the interpreter by forcing it to convert each string to a binary numeric representation, and then compare each bit, and then return the result as an evaluation of the bit comparison.  Because I was "casting" the result as a String (in SSJS, I believe this is technically being cast as a java.lang.String object, but I'm too new at this to be certain), the resulting value for the tag variable was either "0" or "1" -which is not at all what I wanted.

So in addition to learning the cool new stuff about XPages, I also have to unlearn instinctive muscle-memory habits.  This was the first in what I expect to be a long line of "gotcha" moments.

For those of you still following along, the correction is to change line #10 to:

10. var
tag:String = "unid.recordID." + queueID;


No Comments Found

Discussion for this entry is now closed.