Setting Catalog Item Variables from URL Parameters - ben-vargas/servicenow-wiki GitHub Wiki
When creating a custom ServiceNow Catalog Item, you may want to pre-populate certain variables based on URL parameters. For example, you might include parameters like sysparm_request_type
or sysparm_capability
in a custom URL to direct users to a specific Catalog Item view with preselected values.
However, using javascript:RP.getParameterValue('parameter_name')
directly in variable default values can fail under certain circumstances—such as when running a multi-step wizard or if the variable is being used in non-Employee Service Center (ESS) contexts.
This article shows how to properly retrieve URL parameters and set variable values on load, ensuring a smoother user experience and consistent behavior regardless of the environment.
Example Scenario
Custom URL Example:
com.glideapp.servicecatalog_cat_item_view.do?sysparm_id=aae7e9606f5221007af78e354b3ee455&sysparm_request_type=Tech%20Ops%20Tools&sysparm_capability=ServiceNow
In this URL:
sysparm_request_type
andsysparm_capability
define values you want to set on variables in the Catalog Item form.- Initially, you might try
javascript:RP.getParameterValue('sysparm_request_type')
in a variable’s default value, but this can fail in certain contexts.
javascript:RP.getParameterValue()
May Fail
Why - Wizards or Non-ESS Scenarios:
The functionRP.getParameterValue()
may not always return the expected values when used as a variable default script, especially in a wizard or when accessed outside of the ESS portal. - Timing & Rendering Issues:
The Catalog Item form may load in a manner that prevents these values from being set at the right time, causing empty or incorrect defaults.
Recommended Approach: OnLoad Client Script
Instead of relying on the variable’s default value to pull from URL parameters, use an onLoad Client Script that:
- Extracts parameter values from
window.location.href
. - Cleans and decodes them.
- Sets the variable values directly via
g_form.setValue()
.
This approach ensures that the variables are set after the form is fully loaded, increasing reliability.
Example Code (OnLoad Client Script)
Client Script Configuration:
- Type: OnLoad
- Applies to your Catalog Item
Script:
function onLoad() {
// Helper function to retrieve a parameter value from the current URL
function getParmVal(name) {
name = name.replace(/[\[]/,"\\[").replace(/[\]]/,"\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.href);
if (results)
return unescape(results[1]);
try {
var topWindow = getTopWindow();
if (topWindow == window)
return '';
results = regex.exec(topWindow.location.href);
if (results)
return unescape(results[1]);
return "";
} catch (e) {
jslog('Error in getParmVal: ' + e.name + ': ' + e.message);
}
}
// Attempt to set request_type variable from sysparm_request_type parameter
var requestType = getParmVal('sysparm_request_type');
if (requestType) {
var typeDecoded = requestType.replace(/\+/g, ' ');
g_form.setValue('request_type', typeDecoded);
}
// Attempt to set capability variable from sysparm_capability parameter
var capability = getParmVal('sysparm_capability');
if (capability) {
var capabilityDecoded = capability.replace(/\+/g, ' ');
g_form.setValue('capability', capabilityDecoded);
}
}
What This Script Does:
- getParmVal(name): Extracts the specified parameter from the current page URL or the top window URL. It returns an empty string if the parameter doesn’t exist.
- Parameter Decoding: The code replaces
+
signs (URL-encoded spaces) with actual spaces, ensuring a clean, user-friendly value. - Setting Variables: Uses
g_form.setValue()
to assign the decoded parameter values to therequest_type
andcapability
variables.
Additional Notes
-
Server-Side AJAX Calls:
If variables depend on values retrieved from server-side (e.g., via GlideAjax) after settingrequest_type
, consider triggering the server call within the same onLoad script or in anonChange
Client Script for therequest_type
variable. -
Error Handling & Logging:
The script usesjslog()
for error logging. For debugging, consider addingalert()
orconsole.log()
messages (in non-production environments) to verify that parameters are retrieved correctly. -
Adapting to Other Parameters:
You can easily adapt this code to pull additional parameters by adding more calls togetParmVal()
and usingg_form.setValue()
on different variables.
Conclusion
By using an onLoad Client Script and parsing URL parameters through window.location.href
, you can reliably set Catalog Item variables. This method avoids potential issues with javascript:RP.getParameterValue()
in certain scenarios, providing a more robust and user-friendly experience for catalog item configuration.