XPages SSJS: Beware – context.getURL() does not do what it says on the tin!

In working with the EXTJS grids I recently noticed an issue with the way I was getting the URL submitted to a REST service. The intention was to ready the query_string parameters created by the EXTJS grid filters, turn them into a notes understandable search string format and query the REST service. Seemed simple enough……

So to get the query_string initially I used context.getUrl() in the search property of the REST service. This returned the following when printed to the console (I have added carriage returns for clarity).

The important thing to look for is the filter[0][data][value]

print(context.getUrl().toString())

03/04/2013 12:36:20 PM HTTP JVM:
count=25&keys&_dc=1362420087840
&filter[0][field]=txtStatus
&filter[0][data][type]=list
&filter[0][data][value]=Sent
&page=1&start=0&limit=25&sort=GKO_DateLastMod&dir=DESC

The problem is that that was not the URL calling the REST service. The URL actually contains two parameters which are the same, but have different values.

…thedatabase.nsf/xRestService.xsp?count=25&keys=&_dc=1362420087840
&filter[0][field]=txtStatus
&filter[0][data][type]=list
&filter[0][data][value]=Draft
&filter[0][data][value]=Sent
&page=1&start=0&limit=25&sort=GKO_DateLastMod&dir=DESC

It would appear that the XSPContext object was somehow unique-ing the incoming parameters and losing the first one. Mild panic ensued and then I quickly realized there was another way to get the URL string using the facesContext…..(thanks to Julian Buss for the code)

var urlstring = facesContext.getExternalContext().getRequest().getQueryString()
var resultstring=java.net.URLDecoder.decode(urlstring,"UTF-8");
print(resultstring)

03/04/2013 12:36:20 PM HTTP JVM:
…thedatabase.nsf/xRestService.xsp?count=25&keys=&_dc=1362420087840
&filter[0][field]=txtStatus
&filter[0][data][type]=list
&filter[0][data][value]=Draft
&filter[0][data][value]=Sent
&page=1&start=0&limit=25&sort=GKO_DateLastMod&dir=DESC

This gave me the full string I was looking for which I was then able to conquer the world with (well OK the REST service anyway)

In Summary

using context.getUrl() on a URL with query_string that looks like this

&filter[0][data][type]=list&filter[0][data][value]=Draft&filter[0][data][value]=Sent

returned this which is wrong

&filter[0][data][type]=list&filter[0][data][value]=Sent

Don’t use: context.getUrl() to get the URL + Query_String
Use: facesContext.getExternalContext().getRequest().getQueryString()

Advertisements

4 thoughts on “XPages SSJS: Beware – context.getURL() does not do what it says on the tin!

    • context.getUrlParameter was actually where I got started – only returns the one value and not both – these all use the XSPContext object so I assume suffers from the same issue in the same way

      • Ahh.. Thanks for the tip. Great to know 🙂

        When doing remote filtering and sorting in ExtJS grid using ExtLib REST service (xAgent). I end up with this syntax (notice the query part):

        http://server/db/xRestService.xsp/Read?view=lupREST&page=1&start=0&limit=500&sort=$RegDato&dir=DESC&query=%5B{“property”:”$StatusTekst”,”value”:”Registrert”,”type”:”combo”,”operator”:”eq”},{“property”:”$RegDato”,”value”:”01.01.2013″,”type”:”date”,”operator”:”gte”}]

        in the REST service search method I call the function buildFTQuery(context.getUrl().getParameter(“query”))

        Not the prettiest code…

        function buildFTQuery(cntxt){

        var param=””;
        var criteriaArray=new Array();
        var criteria=””;

        param=unescape(cntxt.getUrlParameter(‘query’));

        if(param!=””){
        var JSONArr = eval(‘(‘ + param + ‘)’);
        for (i in JSONArr)
        {
        criteria = JSONArr[i][“property”];

        if(JSONArr[i][“type”]==”date”){
        if(JSONArr[i][“operator”]==”eq”){
        criteriaArray.push(‘[‘ + criteria.substr(1) + ‘] = ‘+ JSONArr[i][“value”]);
        }else if(JSONArr[i][“operator”]==”gte”){
        criteriaArray.push(‘[‘ + criteria.substr(1) + ‘] >= ‘+ JSONArr[i][“value”]);
        }else if(JSONArr[i][“operator”]==”lte”){
        criteriaArray.push(‘[‘ + criteria.substr(1) + ‘] <= '+ JSONArr[i]["value"]);
        }else if(JSONArr[i]["operator"]=="ne"){
        criteriaArray.push('[' + criteria.substr(1) + '] ‘+ JSONArr[i][“value”]);
        }else{
        criteriaArray.push(‘[‘ + criteria.substr(1) + ‘] = ‘+ JSONArr[i][“value”]);
        }

        }else if(JSONArr[i][“type”]==”int”){
        if(JSONArr[i][“operator”]==”eq”){
        criteriaArray.push(‘[‘ + criteria.substr(1) + ‘] = ‘+ parseInt(JSONArr[i][“value”]));
        }else if(JSONArr[i][“operator”]==”gte”){
        criteriaArray.push(‘[‘ + criteria.substr(1) + ‘] >= ‘+ parseInt(JSONArr[i][“value”]));
        }else if(JSONArr[i][“operator”]==”lte”){
        criteriaArray.push(‘[‘ + criteria.substr(1) + ‘] <= '+ parseInt(JSONArr[i]["value"]));
        }else if(JSONArr[i]["operator"]=="ne"){
        criteriaArray.push('[' + criteria.substr(1) + '] ‘+ parseInt(JSONArr[i][“value”]));
        }else{
        criteriaArray.push(‘[‘ + criteria.substr(1) + ‘] = ‘+ parseInt(JSONArr[i][“value”]));
        }

        }else if(JSONArr[i][“type”]==”combo”){
        criteriaArray.push(‘[‘ + criteria.substr(1) + ‘] contains “‘+ JSONArr[i][“value”]+'”‘);

        }else{
        criteriaArray.push(‘[‘ + criteria.substr(1) + ‘] contains *’+ JSONArr[i][“value”]+’*’);
        }
        }
        }

        if(criteriaArray.length>0){
        return “(” + criteriaArray.join(“) AND (“) + “)”;
        }else{
        return criteriaArray
        }
        }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s