Service Portal and client scripts

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

You might want to mark a client script compatible with both desktop and mobile, but still do something different depending on the runtime. You can use this script:
  if (window === null)
    // Write your mobile compatible code here
    // Write your desktop compatible code here

Unsupported client scripting globals

Widget client controllers are full Angular controllers and are not subject to the unsupported client script globals listed here. Use jQuery and Angular as needed.

The following globals and APIs are unavailable in client scripts and catalog client scripts used in the Service Portal:

  • window
  • document
  • $
  • jQuery
  • $$
  • $j
  • angular

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:

  • $
  • $

For more information, see Replace a Service Catalog form script with a widget.

Supported client side APIs

These are the supported client scripting APIs you can use in onLoad, onChange, and onSubmit client scripts.

For detailed class and method information, see the API reference.

Class Available methods
  • addDecoration(fieldName, icon, title)
  • addErrorMessage(message)
  • addInfoMessage(message)
  • addOption(fieldName, value, label, index)
  • clearMessages()
  • clearOptions(fieldName)
  • clearValue(fieldName)
  • getActionName()
  • getBooleanValue(fieldName)
  • getDecimalValue(fieldName)
  • getEncodedRecord()
  • getFieldNames()
  • getIntValue(fieldName)
  • getLabel(fieldName)
  • getReference(fieldName, callback)
  • getRelatedListNames()
  • getSectionNames()
  • getSysId()
  • getTableName()
  • getValue(fieldName)
  • hasField(fieldName)
  • hideAllFieldMsgs(type: "info | error")
  • hideErrorBox(fieldName)
  • hideFieldMsg(fieldName, clearAll)
  • hideRelatedList(listTableName)
  • hideRelatedLists()
  • isMandatory(fieldName)
  • isNewRecord()
  • isReadOnly(fieldName)
  • isVisible(fieldName)
  • removeDecoration(fieldName, icon, title)
  • removeOption(fieldName, value)
  • save()
  • serialize(onlyDirtyFields)
  • setFieldPlaceholder(fieldName, placeholder)
  • setLabel(fieldName, label)
  • setMandatory(fieldName, isMandatory)
  • setReadOnly(fieldName, isReadOnly)
  • setSectionDisplay(sectionName, isVisible)
  • setValue(fieldName, value, displayValue)
  • setVisible(fieldName, isVisible)
  • showErrorBox(fieldName, message, scrollForm)
  • showFieldMsg(fieldName, message, type: "info | error", scrollForm)
  • showRelatedList(relatedTableName)
  • showRelatedLists()
  • submit(submitActionName)
Note: Using the variables.var_name notation with the g_form API is not supported in Service Portal. g_form as a global object cannot be used in a widget client controller or in a UI script.
  • get(fieldName)
  • addItem(value, displayValue)
  • removeItem(value)
  • reset()
  • setQuery(queryString)
  • setDefaultOperator(operator)
  • getDefaultOperator()


new GlideAjax(scriptIncludeName)
  • addParam (name, value)
  • getParam (name)
  • getXML(callback)
  • getXMLAnswer(callback)
  • getJSON(callback)
  • setErrorCallback(errorCallback)
  • getURL()
  • getParams()
  • execute()
  • successCalback(data, status, xhr)
  • errorCallback(xhr)
  • setScope(scope)
Note: Because the mobile platform does not allow synchronous GlideAjax calls, the getXMLWait() method in a GlideAjax call will not work in the Service Portal. Instead, use one of the following:
  • getXML(Function callback)
  • getXMLAnswer(Function callback)
new GlideRecord(tableName)
  • addQuery(encodedQuery)
  • addQuery(fieldName, operator, value)
  • getEncodedQuery()
  • get(id)
  • getTableName()
  • hasNext()
  • insert(callback)
  • gotoTop()
  • next()
  • loadRow(rowObj)
  • getValue(fieldName)
  • setValue(fieldName, value)
  • isDotWalkField(fieldName)
  • addOrderBy(fieldName)
  • setDisplayFields(fieldNames)
  • query(callback)
  • setRows(rowsArray)
  • setTableName(tableName)
  • setLimit(maxInt)
  • getLimit()

getMessage(messageKey, callback)

Usage examples

The g_list global helps you set the filter of a glide list element or a list collector variable. Use this API in place of the g_filter API on desktop client scripts.
function onLoad() {
  var myListCollector = g_list.get("my_list_collector");
  myListCollector.addItem('3a700d39af5f4fc0aab978df90f4c692', 'Power Supply');
  myListCollector.addItem('1cb93419a3a248318da8f814140b42f6', 'Backpack');
g_service_catalog is only available in Service Portal service catalog item scripts. Use this API to know if your catalog item script is run as part of an order guide or on its own.
function onLoad() {
  if (window) // if CMS, don't run this

   // g_service_catalog api for Service Portal and Mobile
   var isOrderGuide = g_service_catalog.isOrderGuide();
   g_form.setValue("is_order_guide", isOrderGuide ? "Yes!" : "Nope :(");