Using Business Rules and Client Scripts to Manage URL Parameters - ben-vargas/servicenow-wiki GitHub Wiki

In ServiceNow, manipulating data and behavior based on the URL parameters can streamline user workflows. This article covers two scenarios:

  1. Server-Side Control with Business Rules: Dynamically filtering queries based on URL parameters passed to UI Actions.
  2. Client-Side Control with Catalog Client Scripts: Automatically populating form fields or catalog variables from URL parameters.

Server-Side Example: Business Rule and UI Actions

Use Case

When a particular UI action triggers a view of a related list, you may want to enforce certain query conditions that are not applied by default. For example, consider a scenario where the sysparm_target parameter in the URL determines which records are displayed. You can use a Business Rule to detect this parameter and add queries on the fly.

Sample Script (Business Rule onBefore)

function onBefore(current, previous) {
    // Retrieve the URL and target parameters
    var uri = gs.action.getGlideURI();
    var target = uri.get('sysparm_target');

    // If the target references 'x_teth_crm_account.attributes_list', 
    // ensure that only records with type='Account Attributes' are shown
    if (target.indexOf('x_teth_crm_account.attributes_list') >= 0) {
        current.addQuery('type', 'Account Attributes');
    }
}

How This Works:

  • The rule runs before querying the records.
  • It checks the sysparm_target parameter in the URL.
  • If it finds the specified substring, it modifies the query, ensuring only records of type Account Attributes are returned.

Benefits:

  • Dynamically tailor the results without hardcoding filter conditions on the list or needing multiple configurations.
  • Centralize logic in a Business Rule, making it easier to maintain.

Client-Side Example: Catalog Client Scripts

Use Case

Catalog items often need to pre-populate variables based on URL parameters. For example, a support team might send a custom link to a user that includes pre-filled information (e.g., category, comments) in the query string. A client script can parse these parameters and set corresponding catalog variables automatically.

Basic Client Script Example

function onLoad() {
    // Extract values from URL parameters using getParmVal()
    var cat = getParmVal('sysparm_category');
    var comments = getParmVal('sysparm_comments');

    if (cat) {
        g_form.setValue('my_category_variable', cat);
    }
    if (comments) {
        g_form.setValue('my_comments_variable', comments);
    }
}

function getParmVal(name) {
    var url = document.URL.parseQuery();
    if (url[name]) {
        return decodeURI(url[name]);
    } else {
        return;
    }
}

Key Points:

  • The onLoad function runs when the catalog item form loads.
  • getParmVal parses the current URL, retrieving and decoding the given parameter.
  • Variables on the form (e.g., my_category_variable, my_comments_variable) are set from the parameters.

Generalized Approach for All Variables

If you want to pass multiple parameters that directly map to catalog variables (without specifying each one individually), you can generalize the code:

function onLoad() {
    var url = document.URL.parseQuery();

    // Iterate through each parameter
    for (var key in url) {
        // Skip sysparm_* parameters
        if (key.toString().indexOf('sysparm') < 0) {
            var val = decodeURI(url[key]);
            g_form.setValue(key, val);
            console.debug("Key: " + key + " -- Value: " + val);
        } else {
            console.debug("Key Skipped: " + key);
        }
    }
}

What This Does:

  • Loops through every parameter in the URL.
  • Filters out sysparm_* parameters commonly used by ServiceNow internally.
  • For each non-sysparm parameter, tries setting a form variable that matches the parameter name.
  • This is especially useful if you have multiple variables that need to be set dynamically, without writing separate code for each.

Best Practices

  • Server-Side Logic (Business Rules):

    • Keep rules simple and targeted.
    • Always test changes in a non-production environment first.
    • Use logging (gs.info(), gs.debug()) to track and troubleshoot parameter handling.
  • Client-Side Logic (Catalog Scripts):

    • Validate the existence of variables before setting them.
    • Consider security implications. Do not trust URL parameters blindly if sensitive info is involved.
    • For multiple parameters, ensure the variable naming conventions align with parameter names for a seamless experience.

Summary

By leveraging server-side Business Rules and client-side Catalog Client Scripts to interpret and use URL parameters, you can create a more flexible, user-friendly environment. Whether itโ€™s enforcing query conditions on related lists or populating catalog item variables automatically, these techniques help you streamline navigation, data entry, and user experience in ServiceNow.