How can i script a BR to search for a substring in an incident's short description?

Ashirav
Tera Expert

Hello All,

I need that if a specific incident's short description is containing incident number, then that incident number should be stored in a variable.

Is there a way in scripting how I can extract that text from the short description? The words are at the 5th position index in the short description.

 

I tried the following in background script but its not working:-

var gr = new GlideRecord('incident');
gr.addQuery('number','INC0533650');
gr.query();
while(gr.next())
{
var res = sgr.short_description.substring(5, 10);
gs.log("Found it");
gs.log(res);
}

1 ACCEPTED SOLUTION

@Ashirav 

Script I tried with your short description and got the incident number

var str = 'INC# INC0528025 / ATT_NI_RMM_VC_ABCDE  / 1 - Critical / New / GENERIC DEVICE / test';

var regex = new SNC.Regex('/INC\\d{7}/im');

var match = regex.match(str);

gs.info('Incident number->' + match);

Output:

find_real_file.png

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

15 REPLIES 15

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

You can use the contains query on short_description.

Can you explain where is this script required

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur,

I need the script in a business rule.

I need to fetch the incident number from short description and run a query on sys_email table based on that Incident number fetched. I have never fetched or stored a string in servicenow so i cant find it..

asifnoor
Kilo Patron

Hi,

you can try like this.

//short_desc is a variable in which short description present
if(short_desc.indexOf("INC")>-1) {
  short_desc = short_desc.split(" ");
  var number = short_desc[4]; //as you mentioned number will be in 5th position
  gs.print(number);
}

Thank you for the reply asifnoor.

I used the code in the background script, but it gave the output as "undefined".

I used the following:

var gr = new GlideRecord('incident');
gr.addQuery('number','INC0528025');
gr.query();
while(gr.next())
{
gr.short_description = gr.short_description.split("/");
var n = gr.short_description[4]; //as you mentioned number will be in 5th position
gs.log(n);
gs.log("Found it");

}

 

The output was:

*** Script: undefined
*** Script: Found it

 

The short description contains:

INC# INC0528025 / ATT_NI_RMM_VC_ABCDE  / 1 - Critical / New / GENERIC DEVICE / test