The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Vaishnavi Lathk
Mega Sage
Mega Sage

In ServiceNow, URL parameters are commonly used to pass data between forms, catalog items, or during redirection between records. Extracting these parameters can be crucial in various use cases, especially when you need to pre-fill fields or control the behavior of a form based on the information passed through the URL. Understanding how to efficiently grab URL parameters in ServiceNow can significantly enhance the functionality and user experience of your applications.

Common Use Cases for Grabbing URL Parameters

URL parameters are widely used in ServiceNow for a variety of tasks. Some common use cases include:

  1. Redirecting from an Incident Record to a Catalog Item: When redirecting from an Incident record to a Catalog Item, you may want to pass certain details, such as the Incident’s short description, caller information, or any other specific details related to the Incident. This helps create a seamless experience, allowing users to see relevant information when interacting with the catalog item.
  2. For example, you might want to pass the short_description and caller_id as URL parameters when redirecting to a catalog item. By passing these parameters in the URL, you can pre-populate certain fields on the catalog item form.
  3. Creating Requests from Incidents: When creating requests from incidents, it’s often necessary to pass additional information like the caller’s details, the short description of the incident, or any other relevant data to the request. These parameters can then be used to automatically fill out fields such as the “Requested For” field, which is often populated with the caller’s information.
  4. Linking Between Custom Forms: Another common scenario is linking custom forms or tables and passing data between them. URL parameters allow for this transfer of information, making it easier to maintain context between different forms or tasks within ServiceNow.

Example of a ServiceNow URL with Parameters

Consider a URL like the following that might be used in ServiceNow

https://instanceName.service-now.com/incident.do?sys_id=-1&sysparm_query=active=true&sys_myParameter=hello

This URL contains the following parameters:

  • sys_id: Identifies the unique record in ServiceNow (in this case, an incident record).
  • sysparm_query: Specifies a query to filter records (e.g., to show only active incidents).
  • sys_myParameter: A custom parameter that could be used for passing any value (in this case, the value hello).

If you need to extract the value of the sys_myParameter parameter from this URL, you can use different methods depending on the context in which the script is running (such as whether it's in the global scope or the portal).

Extracting URL Parameters Using GlideURLV3 (Preferred Method)

ServiceNow provides the GlideURLV3 API to work with URLs and extract parameters efficiently. The GlideURLV3 API offers the getParam method, which simplifies extracting URL parameters. This method is commonly used in non-portal environments, such as within ServiceNow forms or custom applications.

Here’s an example of how to use the GlideURLV3 API in an onLoad client script to extract a parameter:

var url = new GlideURL(window.location.href);
var myParam = url.getParam('sys_myParameter');

In this example:

  • window.location.href is used to get the full URL of the current page.
  • The getParam method is called to extract the value of the parameter sys_myParameter.

This approach is simple and works seamlessly in non-portal environments, where GlideURLV3 is fully supported.

Handling URL Parameters in the ServiceNow Portal (Legacy Approach)

While GlideURLV3 works well in ServiceNow forms and custom applications, it is not supported within the ServiceNow portal framework. In portal pages, you’ll need to use a more traditional JavaScript method to extract parameters from the URL. This can be done using the native window.location object and regular expressions.

Here’s an example of how to retrieve a parameter from the URL using legacy JavaScript methods in a portal:

function getParameterByName(name) {
var url = window.location.href;
name = name.replace(/[\[\]]/g, '\\$&'); // Escape any special characters
var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)');
var results = regex.exec(url);
if (!results) return null; // If parameter not found
if (!results[2]) return ''; // If parameter is empty
return decodeURIComponent(results[2].replace(/\+/g, ' ')); // Decode and return the parameter value
}
var myParam = getParameterByName('sys_myParameter');

In this example:

  • The getParameterByName function uses a regular expression to search for a parameter in the URL query string.
  • If the parameter is found, it is decoded (to handle URL-encoded characters) and returned.

This method works in portal pages where the GlideURLV3 API is not available, allowing you to extract URL parameters even in the portal context.

Key Considerations for URL Parameter Extraction

When working with URL parameters in ServiceNow, there are a few important points to keep in mind:

  1. Parameter Encoding and Decoding: URL parameters may contain special characters (such as spaces or non-ASCII characters) that need to be properly encoded and decoded. Make sure to use decodeURIComponent() in your scripts to handle the decoding of these characters.
  2. Parameter Names: Ensure that you use the correct parameter names when trying to retrieve them from the URL. Inconsistent naming or typos in the parameter names can cause the script to fail or return incorrect results.
  3. Scope Considerations: The GlideURLV3 API works well in non-portal environments (such as custom forms and applications), but if you are working within the ServiceNow portal, you’ll need to rely on the legacy JavaScript methods described above.
  4. Security Implications: When passing sensitive information via URL parameters, be mindful of security considerations. URL parameters are visible in browser address bars and may be captured in logs or history, so avoid passing sensitive data like passwords or confidential user information in URLs.

Conclusion

Grabbing URL parameters in ServiceNow is a powerful technique that can be used to pre-fill fields, pass context between records, and enhance the user experience by providing seamless transitions between forms or catalog items. By leveraging the GlideURLV3 API for non-portal environments and using legacy JavaScript methods for the ServiceNow portal, you can ensure that your ServiceNow applications are flexible and robust, with easy access to URL parameters whenever necessary.

Understanding how to work with URL parameters is a key skill for any ServiceNow developer, and mastering these techniques can help you build more dynamic and user-friendly applications.

Key Takeaways:

  • ServiceNow URL Parameters: Essential for passing data between forms, catalog items, and records.
  • GlideURLV3 API: The preferred method for URL parameter extraction in non-portal environments.
  • Legacy JavaScript Methods: Required for extracting URL parameters in ServiceNow portals.
  • Security: Always be cautious when passing sensitive data through URL parameters.

By understanding these techniques and methods, you can effectively manage URL parameters within your ServiceNow instance and improve the overall functionality and user experience of your applications.

#ServiceNowCommunity #ServiceNowDeveloper #ServiceNow

 
Regards,
Vaishnavi Lathkar
 
 
 
 
 
 
Version history
Last update:
‎11-14-2024 05:56 AM
Updated by:
Contributors