Parsing value returned from RP.getParameterValue with a regular expression
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2015 07:19 AM
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]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2015 01:35 AM
Thank you for your explanation Travis Toulson. Personally I never use RP.getParameterValue to get a parameter from the URL. The way I do it is accessing directly to the parameter as any other Jelly variable (jelly="true" allows Jelly context variables to be referenced in the script) and without using JEXL.
Simple and efficient, no?
<?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 uri = jelly.sysparm_search;
</g:evaluate>
${typeof uri}
</j:jelly>
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-16-2015 09:19 AM
That's a sharp approach, Roberto Alvarez Alonso! I like it and I will definitely be tucking that into my toolbox for later. Thank you for sharing!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-21-2017 01:25 PM
The ServiceNow Wiki content is no longer supported. Updated information about this topic is located here: UI Pages
Visit http://docs.servicenow.com for the latest product documentation
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2015 08:36 AM
RP does not need to be put in the JEXL expression and in fact should not be.
The reason this works is that Jelly will replace "${RP.getParameterValue('sysparm_search')}" with "Support, Office 12345 ,,, Help Desktop" during Jelly evaluation. In this case it works but as per the wiki you should avoid using JEXL expressions within g:evaluate since it can cause memory resource issues and affect performance over time.
I would strongly advise reconsidering.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2015 08:30 AM
Hi Nick,
RP.getParameterValue() returns a Java string. Likewise, calling toString() returns a Java string. What you want is a javascript string, so to perform that type conversion, you can change the first line in the convert function from
Old
string = string.toString();
New
string = string + '';
That will typecast the Java string to Javascript, allowing the use of regex on the string variable.
A sample so you can see for yourself if you wish, create a new UI Page for testing and put the following in the XML:
<?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 uri = RP.getParameterValue('sysparm_search').toString();
</g:evaluate>
${typeof uri}
</j:jelly>
Navigate to the <ui page name>.do?sysparm_search=Support,%20Office%2012345%20,,,%20Help%20Desktop:
You will see "object" displayed on the page indicating that this is a Javascript object (this is the typeof uri in the script above).
Now change line 4 to use the + '' method mentioned above and navigate to the page again.
You will now see "string" displayed on the page indicating that this is a Javascript string.
I hope this helps, please let me know if you need additional assistance.
Kind regards,
Travis