H

ere's a useful script I've used before to grab parameters from a URL in Service-now.com. Simply pass the proper noun of the parameter into the role and it volition render the corresponding parameter value.

So if yous had a Service-now.com URL that looked like this…
https://demo.service-at present.com/incident.do?sys_id=-1&sysparm_query=active=true&sys_myparm=abcde

You could get the value of the 'sys_myparm' URL parameter by calling the function below similar this…

var myparm = getParmVal( 'sys_myparm' ) ;

Hither's the 'getParmVal' function…

office getParmVal(name) {
var url = document.URL.parseQuery ( ) ;
if (url[name] ) {
return decodeURI(url[proper noun] ) ;
}
else {
return ;
}
}

I've often used this to automatically populate itemize variables based on a redirect URL. For example, if I were redirecting to a catalog detail and I wanted to pass in a couple of variable values I could construct my URL similar this…

var url = 'com.glideapp.servicecatalog_cat_item_view.do?sysparm_id=e826b8c50a0a3c1e019410bb1031d102' + '&sysparm_category=hardware' + '&sysparm_comments=Hello World!!!' ;

This URL contains 3 major pieces of data…

  1. Pointer to the catalog item – identified by item sys_id ('com.glideapp.servicecatalog_cat_item_view.do?sysparm_id=e826b8c50a0a3c1e019410bb1031d102')
  2. A parameter containing a category value ('&sysparm_category=hardware')
  3. A parameter containing a comments value ('&sysparm_comments=How-do-you-do World!!!')

By passing parameters in through the catalog detail URL you lot tin can use an 'onLoad' catalog client script (set against the item or a variable set up) to pull those parameters out and populate them into your itemize particular variables like this…

function onLoad( ) {
//Populate the variables with the parameters passed in the URL
//Use the 'getParmVal' part below to get the parameter values from the URL
var cat = getParmVal( 'sysparm_category' ) ;
var comments = getParmVal( 'sysparm_comments' ) ;
if (true cat) {
g_form.setValue ( 'my_category_variable' ,cat) ;
}
if (comments) {
g_form.setValue ( 'my_comments_variable' ,comments) ;
}
}

function getParmVal(name) {
var url = document.URL.parseQuery ( ) ;
if (url[proper noun] ) {
return decodeURI(url[name] ) ;
}
else {
render ;
}
}

When working in the Service Portal, ServiceNow severely restricts the objects available for utilize in client scripts, causing scripts like this to break. Brad Tilton provided this workaround on the ServiceNow community for the 'getParameterValue' part that is designed to work around this outcome.

function getParameterValue(name) {
proper noun = proper noun.replace ( /[\[]/ , "\\ \[" ).replace ( /[\]]/ , "\\ \]" ) ;
var regexS = "[\\?&]" + proper name + "=([^&#]*)" ;
var regex = new RegExp(regexS) ;
var results = regex.exec (top.location ) ;
if (results == zero ) {
return "" ;
} else {
render unescape(results[ 1 ] ) ;
}
}

Delight note that this code snippet should be used within a client script or catalog client script on a form yous're rendering through the Service Portal. If you're working within a widget and want to use a url parameter you would want to apply angular's $location service.