Parsing value returned from RP.getParameterValue with a regular expression

nmartinez
Kilo Contributor

I am trying to pull a URL parameter into a Jelly UI page, and parse out all numeric and non-letter values.

For testing purposes; if I hard-code a string value, the regular expression works correctly. However if I try to parse the value pulled form RP.getParameterValue the script include, it fails at the regular expression line. It also fails if I include the regular expression within the UI page too. SN has their own built in regex API (SNC.Regex) which also can't process values pulled from RP.getParameterValue. I've tried converted the returned value to a string, with no luck either.


[code]

<?xml version="1.0" encoding="utf-8" ?>

<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">

  <g:evaluate jelly="true">

  var stringValue = "Support, Office 12345 ,,, Help Desktop"; // Works    

  //var stringValue = RP.getParameterValue('sysparm_search'); // Doesn't work                        

  var siRegEx = new RegExString();

  returnValue = siRegEx.convert(stringValue);

  arrayResults = returnValue.split(',');

  </g:evaluate>          

  <br/>Raw Values: ${stringValue}

  <br/>Returned Values: ${returnValue}

  <br/>Array Values:

  <j:forEach items="${arrayResults}" var="jvar_value">

  ${jvar_value}

  </j:forEach>

</j:jelly>

[/code]


[code]

var RegExString = Class.create();

RegExString.prototype = {

      initialize: function() {

      },

  convert : function(string) {

  string = string.toString();

  gs.log('Debug string: ' + string);

  if (string) {

  newString = string.replace(/[^a-z]+/gi, ',');

  gs.log('Debug newString: ' + newString)

  return newString;

  }

  },

         

      type: 'RegExString'

};

[/code]

10 REPLIES 10

ralvarez
Tera Guru

If this works


var stringValue = "Support, Office 12345 ,,, Help Desktop";



And this one doesn't


var stringValue = RP.getParameterValue('sysparm_search');



I would say it's because you are not retrieving correctly the parameter from the URL.


Since RP is a Jelly extension you should use it as follows.


var stringValue = "${RP.getParameterValue('sysparm_search')}";




Cheers,


Roberto


Thank you Roberto, that fixed it!


Personally I prefer to use this way of accessing URL variables:


http://wiki.servicenow.com/index.php?title=UI_Pages#Invoking_a_Page



Cheers,


Roberto


I've submitted feedback to that wiki page.   Hopefully, ServiceNow can clarify by either following the best practice in the example script on that page or by clarifying the best practice so we can understand when it is acceptable to use JEXL within g:evaluate.



Regardless, it is important to understand that the original script above is accessing the URL parameter correctly.   JEXL is not needed to access the RP variable.   So saying that "Since RP is a Jelly extension you should use it a follows: [using JEXL]" is incorrect and could lead others in the community to develop poor practices, especially with that answer being marked as correct.



The true reason the original script was not working is that RP.getParamterValue returns an object (I am assuming a Java string) which a Javascript RegEx will not recognize, creating an error.   The fix, is to convert the returned object into a Javascript string.



In this case, the JEXL approach works (with the caveat that it may be against best practice and could cause memory issues) due to the Jelly Phase 1 JEXL substitution, which causes Jelly to interpret the statement as a string since you surrounded the JEXL with quotes.



The simplest way to convert an object to a string without side effects , and particularly the one returned by RP is to add + '' to the end of the statement.   "Plus quote quote" in javascript performs a typecast from object to string.   You could also use the j2js script include.   Bottom line, this is not actually a Jelly thing, it has to do with how Rhino is interpreting the Javascript and variable types.