URL Parameters

There are a variety of methods available to retrieve the value of Parameters that are being passed in from a URL in ServiceNow. For those of you who are new to this concept, ServiceNow and most all other modern websites/platforms utilize this method to get data from one page to another. As a ServiceNow admin/developer, you will most likely use this functionality to auto-populate form data or possibly as a means of determining “state” based on previous form selections. A common use case is for turning Calls or Incidents into Requests. In this scenario you could pass the Caller as the “Requested for”, and the short description, into a “Describe your need” variable on a Catch all generic catalog item.

Over the years I have found myself repeatedly using the below techniques to grab these parameters so I wanted to make sure there was a single document that everyone could use for a quick reference.

Now on to the good stuff, an example URL parameter that I’m sure you are familiar with is “sys_id”:
http://instance.service-now.com/incident.do?sys_id=2ca8f612914d4140b1782bd13ae71f72

In this case, the parameter is “sys_id” and the value we are after is “2ca8f612914d4140b1782bd13ae71f72”.

To retrieve this from a Client Script (Client side), my mentor here at Crossfuze Solutions (Mark Stanger), has written a cool little function that allows this (click to see the full article):

var myparm = getParmVal('sys_myparm');
function getParmVal(name){
var url = document.URL.parseQuery();
if(url[name]){
return decodeURI(url[name]);
}
else{
return;
}
}

Next, if you are wanting to do the same thing in a CMS/UI Page (Server Side) then you will need to do this in Jelly.

The main way to do this is to utilize the RP object’s method called “getParameterValue”:

RP.getParameterValue('paramName')
RP.getWindowProperties().get('paramName')"

Also, while we’re discussing the RP object, a few other cool methods are as follows:

  • RP.getReferringURL() – returns to URL that got you to this page.
  • RP.isMobile() – returns true if the page is in mobile mode (browsing using a Blackberry, etc.)
  • RP.getParameters() – returns all the parameters passed on the URL (to loop through)

In terms of Jelly, I have had difficulty in the past pulling parameters using the RP methods above especially in UI Macros. Here is how I worked around it just call jelly.”the_name_of_the_param”:


   //This works on any param name including params that dont start with "sysparm" 
   var catItemId = jelly.sysparm_id;
   //Or This works if param contains "sysparm"
   var catItemId = '$[sysparm_id]';

To get Parameters for Business Rules/UI Actions (Server side) you can use the “getEncodedQuery”, and “getGlideURI” methods as follows:

current.getEncodedQuery().indexOf('sys_id=-1') != -1  //is a new record

gs.action.getGlideURI().toString().indexOf('parameter_looking_for_here') == value_to_check

 

ServiceNow has now started getting heavy in the AngularJS space. As such I thought I would include how to get a parameter in Angular (above client side methods still work too). This is usually done using RouteParams:

$routeProvider.when('/view1/:param1/:param2', {
    templateUrl: 'partials/partial1.html',    
    controller: 'MyCtrl1'
});

Then in your controller you would inject $routeParams to use them:

.controller('MyCtrl1', ['$scope','$routeParams', function($scope, $routeParams) {
  var param1 = $routeParams.param1;
  var param1 = $routeParams.param2;
  ...
}]);

 

Fun Tidbit:

As if all of these other methods weren’t enough, there is an additional method that I recently came across in a StackOverflow post that shows a fully customizable approach (in case you were looking to do something unique with URL parsing). There are a number of example solutions on the question, but the post by Haim Evgi had one of the best looking approaches (uses a Regular Expression to parse the URL) so I thought I would convert this into a Script Include for use on the Server side:

ParamsUtil.getValueFromURL = function(param_name,full_url){
   param_name = param_name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
   var regexS = "[\\?&]"+param_name+"=([^&#]*)";
   var regex = new RegExp( regexS );  
   var results = regex.exec( full_url );
   if( results == null ){
      return "";
   }
   else {
      return results[1];
   }
};

Example Usage:

var params_list = ['sys_id','sysparm_query'];
for (i=0; i < params_list.length; i++){
var parameter = params_list[i];
var url = 'https://dev10592.service-now.com/incident_list.do?sys_id=f6cb98490f221a001709c09ce1050ee1&sysparm_query=active%3Dtrue%5Eassigned_toISEMPTY%5Ecaller_id%3D7e85d1bc0f755a401709c09ce1050e97'; 

gs.print(ParamsUtil.getValueFromURL(parameter, url));

Output:Example Output

Referenced Articles:

http://www.servicenowguru.com/scripting/client-scripts-scripting/parse-url-parameters-client-script/

http://wiki.servicenow.com/index.php?title=Extensions_to_Jelly_Syntax

http://stackoverflow.com/a/979997