Catalog Client Script on Record Producer not running

austinn1
Kilo Expert

Edit: Solution at bottom

I'm trying to create a Catalog Client Script that will automatically populate a field called cmdb_ci on the Create Incident Record Producer via a parameter in the URL that is added when the user clicks a <a> in a service portal widget. In my research I found that if the url parameter name is the same as the name of a field on the Record Producer that it should populate automatically, but that is not happening.

Below is the code that generates the URL. It opens a new tab and populates the URL as expected, but the field is not filled.

Beyond that, for testing purposes I added an alert to the beginning of the script just to see if it was running, and found that it is not running at all.

  <p ng-repeat="asset in data.assets">
    <a href="/sp?id=sc_cat_item&sys_id=3f1dd0320a0a0b99000a53f7604a2ef9&cmdb_ci={{asset.sys_id}}"
       target="_blank">{{asset.name}}</a>
  </p>

Here is the script. As you can see I have commented everything but the alert out, but the script still doesn't appear to be running. 

function onLoad() {
	alert("Running Autofill cmdb_ci client script...");
	//var url = new GlideURL(top.window.location.href);
	//var ci = url.getParam('cmdb_ci');
	//if(ci) {
	//	alert(ci + " supplied");
	//	g_form.setValue('cmdb_ci', ci.toString());
	//}
}

Here is a screenshot of the script record. I do not know what the "Applies on Target Record" checkbox does (btw, the descriptions when you hover over the title of fields are not helpful or descriptive in any way)

find_real_file.png

Here is the variable I added to the Record Producer

find_real_file.png

I would appreciate any help on figuring out why the script is not running, and bonus points if you see something wrong with the actual script I'm trying to write.

Solution:

As Pranav Bhagat said below, since this script is for use in the service portal, the UI Type needs to be set to All.

Additionally, there don't seem to be any ServiceNow APIs for getting data from the URL in Service Portal scripts. I found a workaround (regex function with good ole JavaScript DOM) for this on another thread:

function onLoad() {
	var ci = getParameterValue("cmdb_ci");
	g_form.setValue('cmdb_ci', ci);
}

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

Pranav Bhagat
Kilo Sage

If you are running this on Service Portal, you have to Set the UI Type to ALL

View solution in original post

2 REPLIES 2

Pranav Bhagat
Kilo Sage

If you are running this on Service Portal, you have to Set the UI Type to ALL

Thank you for that. Now, do you know a way to read the URL in a catalog client script?

GlideURL and getParmVal are not allowed in these scripts for some reason.