Service Portal and client scripts
Summarize
Summary of Service Portal and client scripts
ServiceNow customers can use client scripts and catalog client scripts within the Service Portal when the UI Type is set toMobile / Service PortalorAll. These scripts work specifically with the Form widget and Service Catalog (SC) Catalog Item widget, rather than widget client controllers. It is important to ensure that only mobile-compatible APIs are used before flagging a script for Mobile or All, since this flag only attempts to run the script in those environments but does not guarantee functionality.
Show less
Client Scripting Considerations
- Runtime detection: Scripts can detect whether they are running in a mobile or desktop environment using
if (window === null), allowing conditional behavior. - Unsupported globals/APIs: Some global objects and APIs such as
$,$$,$j, Angular control, document, jQuery, and window are not available in client scripts within Service Portal. However, widget client controllers are full Angular controllers and can use jQuery and Angular as needed. - Embedded widgets: When embedding widgets in catalog items using Macro variable types, the embedded widget’s client controller can access the field object and catalog item’s
gforminstance via$scope.page.fieldand$scope.page.gform().
Key APIs and Usage in Service Portal Client Scripts
- glist API: Used to manipulate Glide list elements or list collector variables in Service Portal, replacing the desktop
gfilterAPI. It supports resetting lists, setting queries, and adding items dynamically. - gservicecatalog API: Available only in Service Portal service catalog item scripts, this API helps determine if a catalog item is part of an order guide, enabling script logic based on that context.
Supported Client Script Types and APIs
Only certain client scripts are supported in Service Portal, and they must be flagged correctly for the UI type (Mobile / Service Portal or All). Additionally, only client-side APIs compatible with the mobile environment are supported. Existing desktop client scripts may be reused if they rely on supported APIs.
You can use client scripts and catalog client scripts in the Service Portal if the UI Type is set to Mobile / Service Portal or All. Client scripts and catalog client scripts are used with the Form widget and SC Catalog Item widget, as opposed to a widget client controller.
Before flagging a script as Mobile/Service Portal or All, make sure that you are only using the mobile APIs. Setting a client script to Mobile does not ensure that it will work, it simply flags that the script should be attempted by the mobile app or the Service Portal. Many of your existing client scripts can be set to All as long as the API calls are supported by the mobile client scripting environment.
The topics in this section require advanced coding knowledge and an understanding of Service Portal APIs.
Checking desktop vs mobile runtime
if (window === null)
// Write your mobile compatible code here
else
// Write your desktop compatible code hereUnsupported client scripting globals
The following globals and APIs are unavailable in client scripts and catalog client scripts used in the Service Portal:
- $
- $$
- $j
- angular
- control
- document
- jQuery
- window
Embedded widgets & g_form
When using the Service Catalog variable type Macro and Macro with Label, you can pick a widget to embed in a catalog item form. Within the client controller for the embedded widget you can access the field object and catalog item g_form instance using:
$scope.page.field$scope.page.g_form()
Client scripts used with Service Portal
function onLoad() {
var myListCollector = g_list.get("my_list_collector");
myListCollector.reset();
myListCollector.setQuery("active=true^category=8c7b22230b402200b0b02c6317673a62");
myListCollector.addItem('3a700d39af5f4fc0aab978df90f4c692', 'Power Supply');
myListCollector.addItem('1cb93419a3a248318da8f814140b42f6', 'Backpack');
}function onLoad() {
if (window) // if CMS, don't run this
return;
// g_service_catalog api for Service Portal and Mobile
var isOrderGuide = g_service_catalog.isOrderGuide();
g_form.setValue("is_order_guide", isOrderGuide ? "Yes!" : "Nope :(");
}