Servicenow Script For Contains

5 min read Oct 13, 2024
Servicenow Script For Contains

Mastering the Power of "contains" in ServiceNow Scripts

ServiceNow, a powerful platform for IT Service Management (ITSM), relies heavily on scripting to automate tasks and enhance workflows. One essential function within ServiceNow scripts is the ability to check if a string contains a specific substring. This is where the "contains" operator comes into play.

Understanding "contains" in ServiceNow

The "contains" operator is used in ServiceNow scripts to determine if a string includes a particular substring. It is a powerful tool for various scenarios, such as:

  • Filtering records based on specific keywords: Imagine you want to find all incidents containing the word "network" in their description. The "contains" operator can help you filter these incidents efficiently.
  • Validating user input: You can ensure user input adheres to specific requirements by checking if it contains certain characters or patterns.
  • Customizing email notifications: You can personalize email notifications based on the content of a specific field.

Practical Examples: "contains" in Action

Let's delve into some concrete examples to see how "contains" works in ServiceNow scripts:

1. Finding Incidents with a Specific Keyword:

var incidentRecords = new GlideRecord('incident');
incidentRecords.addQuery('short_description', 'LIKE', '%network%');
incidentRecords.query();
while (incidentRecords.next()) {
  gs.info(incidentRecords.number + ' - ' + incidentRecords.short_description);
}

This script retrieves all incident records where the short_description field contains the string "network."

2. Validating User Input:

function validateEmail(email) {
  if (email.contains('@') && email.contains('.')) {
    return true;
  } else {
    return false;
  }
}

This function checks if a provided email address contains both "@" and "." characters, validating its basic format.

3. Customizing Email Notifications:

var emailBody = 'Dear ' + current.user.name + ', \n\n' + 
'This is a notification regarding your incident: ' + current.number + '. \n\n';

if (current.short_description.contains('password')) {
  emailBody += 'Please ensure you follow security guidelines when resetting your password.';
} else {
  emailBody += 'We are working on resolving your issue.';
}

// Send the email with the customized body.

Here, the email notification is customized depending on whether the incident description contains the word "password."

Tips for Effective "contains" Usage

  • Case Sensitivity: "contains" is case-sensitive. If you need to perform case-insensitive comparisons, use the toLowerCase() method.
  • Wildcard Characters: The LIKE operator can be used with the contains function for more flexible matching, using wildcard characters such as "%" (any number of characters) and "_" (single character).
  • Error Handling: Always consider potential errors. If a variable might be empty, use the indexOf() method as an alternative to "contains."

Beyond "contains": Other String Manipulation Tools

While "contains" is valuable, ServiceNow offers other useful functions for working with strings:

  • indexOf(): Returns the position of the first occurrence of a substring within a string.
  • startsWith(): Checks if a string begins with a specific substring.
  • endsWith(): Checks if a string ends with a specific substring.
  • substring(): Extracts a portion of a string based on specified start and end positions.
  • replace(): Replaces occurrences of a specific substring with a new string.

Conclusion

The "contains" operator is a powerful and versatile tool for ServiceNow script developers. It enables efficient data filtering, input validation, and customized workflows. By understanding how to effectively use "contains" and other string manipulation functions, you can create more robust and sophisticated ServiceNow applications.

Featured Posts