Client-side scripts can add dynamic effects and validation to forms. Scripts can apply to service catalog items or variable sets, allowing administrators to use the same functionality that is available on other forms.

You can use client side scripts to:

  • Get or set variable values.
  • Hide or display variables.
  • Make variables mandatory or not.
  • Validate form submission.
  • Add something to the cart.
  • Order something immediately.

Catalog client script considerations

When you create catalog client scripts, be aware of the following considerations.

  • Catalog client scripts run when a user orders an item from the service catalog. Catalog client scripts can also run when variables or variable sets for a catalog item are displayed when a user requests that item.
  • For a variable to be accessible using a catalog client script, it must have a variable name. Variables without names do not appear in the list of available variables.
  • When using standard client scripts on a Requested Item or Catalog Task form, make a note of fields with the same name as variables. If a table field and a variable of the same name are both present on a form, the table field is matched when it is accessed using a script. If this happens, specifically address the variable by naming it variables.variable name. For example: g_form.setValue('variables.replacement', 'false');
  • If you are using record producers to pass variables from the service catalog to other types of records, these variables are made visible in those records with a variable editor, such as the Change Variable Editor UI formatter on Change request forms. You can manipulate these variables using standard client script methods, such as setDisplay, setMandatory,setValue, and getValue.
  • Catalog client scripts can be used for catalog items included in a wizard.
  • You can use the g_form.refreshSlushbucket(fieldName) API to update a list collector variable.

Catalog client script differences

Catalog client scripts are very similar to standard client scripts, with a few important differences.

  • Instead of selecting a table such as Incident for the script, select a catalog item or variable set. As your system may have a large number of catalog items, you should select a catalog item or variable set using a reference field instead of the choice list that the standard Client Script form uses.
  • When using an onChange() catalog client script, it is linked to a particular variable instead of a field. The system automatically populates the variable name selection list with any named variables from the catalog item or variable set selected.

Create a catalog client script

Follow this procedure to create a catalog client script.

  1. Navigate to All > Service Catalog > Catalog Administration > Catalog Client Scripts.
    A list of current custom catalog client scripts appears.
  2. Click New.
  3. Fill in the fields, as appropriate (see table).
  4. Click Submit.

Catalog client script examples

Examples of client scripts to perform common actions.

Example: Get the value of a variable

Use the following syntax to obtain the value of a catalog variable. Note that the variable must have a name. Replace variable_name with the name of the variable.

g_form.getValue('variable_name');

Example: Restrict the number of characters a user can type in a variable

This is an example of a script that runs when the variable is displayed, rather than when the item is ordered.

function onLoad(){
  var sd = g_form.getControl('short_description');
  sd.maxLength=80;
}