petercawdron
Kilo Guru

There's a really nice, simple trick to getting Catalog Client Script to run in both the portal and the back end of ServiceNow and that's by adding the prefix variables whenever the script is running on a regular form.

First, you need to set your scripts so they apply on the target record.

find_real_file.png

Then, within your scripts, add the following three lines to get the prefix required. This will allow the script to self-adjust when it runs, catering for whether it's within the portal or in the back end.

//gains access to the global window object regardless of script isolation or otherwise
var myWin = (0, eval)('this');

//this allows the script to distinguish between when it is running in the portal as opposed to the back end (where the prefix variables. is needed)
var thisPage = myWin.location.href.indexOf('.do')==-1;
var prefix = thisPage ? '' : 'variables.';

//EXAMPLE
//by including prefix+ before your g_form references you can ensure your script will run in both places
if(g_form.getValue(prefix+'u_employment_end_date')==''){
	g_form.setDisplay(prefix+'reason',false);
	console.log('%cEnd date not set','color:navy');
	return;
}

Have fun

Version history
Last update:
‎06-18-2019 06:06 PM
Updated by: