Instance scan column type check

shivaadapa
Tera Expert

Hi
Anyone please provide me column type check examples? 

How to use Regular Expressions patterns?

 

3 REPLIES 3

AshishKM
Kilo Patron
Kilo Patron

Hi @shivaadapa ,

Can you share screen shot of page where you are trying this regEx check, on catalog item variable there is option for validate the field with pre-define regEx else you have to define and check in some script code.

 

Let us know, what's your case.

AshishKMishra_0-1704992345786.png

 

-Thanks,

AshishKMishra


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

Iraj Shaikh
Mega Sage
Mega Sage

Hi @shivaadapa 

 

In ServiceNow, you can use regular expressions (regex) to perform pattern matching against string data. This can be useful for validating data, searching for patterns, or manipulating strings. Below are examples of how you might use regex in different contexts within ServiceNow, including checking column types.

Example 1: GlideRecord and Regex

When you want to check the value of a column in a record for a certain pattern, you can use GlideRecord along with JavaScript's regex capabilities. Here's an example of how you might do this:

 

var gr = new GlideRecord('incident'); // Replace 'incident' with your table name
gr.query(); // Add any query conditions if needed
while (gr.next()) {
    var columnValue = gr.getValue('short_description'); // Replace 'short_description' with your column name
    var pattern = /^[A-Z0-9]+$/; // This is a regex pattern, this one checks for uppercase letters and numbers only

    if (pattern.test(columnValue)) {
        // The column value matches the pattern
        gs.info('Record ' + gr.getDisplayValue('number') + ' matches the pattern.');
    } else {
        // The column value does not match the pattern
        gs.info('Record ' + gr.getDisplayValue('number') + ' does not match the pattern.');
    }
}

 


Example 2: Using Regex in Business Rules or Script Includes

You can also use regex within business rules or script includes to validate data before it's saved to the database or to manipulate data as part of a process.

 

function validateColumnData(columnValue) {
    var pattern = /^[A-Z0-9]+$/; // Regex pattern
    return pattern.test(columnValue);
}

// This function could be called before data is inserted or updated in the database

 


Example 3: Column Type Check

If you're looking to check the type of a column in ServiceNow, you don't necessarily need regex. Instead, you can use the GlideRecord API to get the dictionary attributes of the column, which includes the type.

 

var gr = new GlideRecord('sys_dictionary');
gr.addQuery('name', 'incident'); // Replace 'incident' with your table name
gr.addQuery('element', 'short_description'); // Replace 'short_description' with your column name
gr.query();

if (gr.next()) {
    gs.info('Column type is: ' + gr.getDisplayValue('internal_type'));
}

 

This script will output the type of the column, such as "string", "reference", "choice", etc.

Regular Expressions Patterns

Regular expressions are patterns used to match character combinations in strings. In JavaScript, regex patterns are written between two forward slashes. Here are some basic patterns:

  •  `/abc/`: A sequence that matches the exact character sequence "abc".
  • `/[A-Z]/`: Matches any uppercase letter from A to Z.
  • `/[0-9]/`: Matches any digit from 0 to 9.
  • `/^abc/`: Matches any string that starts with "abc".
  • `/abc$/`: Matches any string that ends with "abc".
  • `/\d/`: Matches any digit (equivalent to `[0-9]`).
  • `/\w/`: Matches any alphanumeric character including the underscore (equivalent to `[A-Za-z0-9_]`).
  • `/\s/`: Matches any whitespace character (spaces, tabs, line breaks).

Remember that using regex in ServiceNow follows the same principles as in JavaScript, so you can refer to JavaScript regex documentation for more complex patterns and usage.

Please mark this response as correct or helpful if it assisted you with your question.

Mark Roethof
Tera Patron
Tera Patron

Hi there,

 

Have a look at my Instance Scan articles:

https://www.servicenow.com/community/developer-blog/325-articles-blogs-videos-podcasts-share-project...

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn