This forum is closed to new posts and responses. Individual names altered for privacy purposes. The information contained in this website is provided for informational purposes only and should not be construed as a forum for customer support requests. Any customer support requests should be directed to the official HCL customer support channels below:

HCL Software Customer Support Portal for U.S. Federal Government clients
HCL Software Customer Support Portal



Apr 14, 2011, 6:04 PM
53 Posts

SSJS help

  • Category: Server Side JavaScript
  • Platform: All
  • Release: 8.5.2
  • Role: Developer
  • Tags:
  • Replies: 4
I've got a fairly simple bit of ssjs that isn't doing what I expect it to, what am I doing wrong?
 
I'm just providing a simple search box, but trying to make it work like a google search so people don't have to enter AND (and I know I need to make this much more complex to take all possibilities into account). I want to remove any ANDs typed, and then add an AND in between every word, so I'm creating an array of each word then splitting it with AND as a separator, then setting a viewScope for use by a display field and the search in a repeat control (this is happening in the beforerenderresponse event)

What I get is a list of the words, separated by a comma, whatever I use as the join separator
 
try {
var sString = new String(context.getUrlParameter("query"));
var sString2 = new String(sString.replace(" and ", " "));
sString2.trim();
var sq = new Array(sString2.split(" "));
var sstring = sq.join(" AND ");
} catch (e) {sstring="Error"
}

viewScope.searchstring = sstring
 
Thanks, Dan
Apr 14, 2011, 7:01 PM
41 Posts
Re: SSJS help
 At first glance, I'm seeing some unnecessary constructors that may be masking the actual behavior:
 
There's no need to pass the sString and sString2 assignments to new String(); the operations themselves return strings, so you're just constructing a new string from an existing string. This doesn't break anything, but is unnecessary. 
 
The Array constructor, on the other hand, seems to be the cause of the behavior you're reporting. As it turns out, the Array constructor is literally never needed:
 
var myArray = new Array(); 
// should be: 
var myArray = [ ]; 
 
Same goes for the Object constructor: 
 
var myObject = new Object(); 
// should be: 
var myObject = { }; 
 
So, to be precise, sString.split(" ") returns an array. Unlike passing a string to new String(), which just turns one string into another string, passing an array to new Array() returns a nested  array. For example:
 
var sq = new Array("one two three".split(" "));
// is equivalent to: 
var sq = [ ["one", "two", "three"] ]; 
 
As a result, sq.join(" AND ") is telling the outer  array to join itself...  since that array only has one value, it simply returns that value: the inner  array.
 
In short, unless I'm reading your code wrong, if you keep the code exactly the same but lose each constructor (or, at a minimum, the Array constructor), it should do what you're expecting it to. 
 
Apr 15, 2011, 9:16 AM
53 Posts
Re: SSJS help
Thanks Tim, this is now working - I've changed my code:
 
 try {
var sString = new String(context.getUrlParameter("query"));
sString.replace(" and ", " ");
sString.trim();
var sq = sString.split(" ");
var sstring = sq.join(" AND ");
} catch (e) {sstring="Error"
}

viewScope.searchstring = sstring;
 
If this can be improved would be good to know, but is much clearer now
Apr 15, 2011, 5:11 PM
47 Posts
Re: SSJS help
 How about using a regex to do the split to avoid an extra string assignment:
 
 try {
  var sq = context.getUrlParameter("query").split(/\s+and\s+|\s+/);
  var sstring = sq.join(" AND ");
} catch (e) {
  sstring="Error"
}
 
You could even avoid the assignment of sq and just go with the single line: 
 
var sstring =  context.getUrlParameter("query").split(/\s+and\s+|\s+/).join(" and ");
 
Just remember that it is  you ( or some other poor soul :-) ) which will have to review/maintain the code, so do whatever is clearest to you. Also, this regex deals with repeated spaces gracefully
 
Rich 
 
 
Apr 15, 2011, 1:11 PM
122 Posts
Re: SSJS help
How about using @Formulas, so @ReplaceSubstring and @Implode?

This forum is closed to new posts and responses. Individual names altered for privacy purposes. The information contained in this website is provided for informational purposes only and should not be construed as a forum for customer support requests. Any customer support requests should be directed to the official HCL customer support channels below:

HCL Software Customer Support Portal for U.S. Federal Government clients
HCL Software Customer Support Portal