Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

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  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

15 REPLIES 15

Hi,

why not use contains query

var gr = new GlideRecord('incident');
gr.addQuery('short_description', 'LIKE' ,'INC0528025');
gr.query();
while(gr.next())
{
gs.log("Found it");

}

Regards
Ankur

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

Hi Ankur thank you for replying,

The incident# in the short description is not fixed, it can be any number and not that incident's number. I wont know what that number is.

I need to fetch that number and use it to query another sys_email table for its emails.

Thats why I cant use it that way.

 

Hi,

you can use regular expression to get the incident number from short_description

like this

var gr = new GlideRecord('incident');
gr.addQuery('number','INC0528025');
gr.query();
if(gr.next())
{
var shortDesc = gr.short_description.toString();

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

var incNumber = regex.match(shortDesc);

gs.log(incNumber);

gs.log("Found it");

}

Regards
Ankur

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

@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  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Thank you Ankur,

I have never used Regex, it is an interesting find and it gave me the incident number. Thanks so much!