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

Glad that my solution worked.

Please remember to mark appropriate responses helpful as well.

Regards
Ankur

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

here is how it should be.

var gr = new GlideRecord('incident');
gr.addQuery('number','INC0528025');
gr.query();
while(gr.next())
{

gs.log(gr.short_description);
var short_description = gr.short_description.toString().split("/");
var n = short_description[4]; //as you mentioned number will be in 5th position
gs.log(n);
gs.log("Found it");

}

Thank you Asifnoor. 

I used this but it gave me the last parameter instead. The short description was 

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

 

I needed to fetch the words INC0528025 which are at 5th position

Ah oaky. When you said 5th position, i thought you meant 5th word.

Then you can go with substring or regex.

Dubz
Mega Sage

Your original method of using substring should work but you need to convert the short description to a string first, you're running a substring method on a glide object. Also, the substring method takes the starting and ending index as its arguments so substring(5,10) will give you the 5 characters between the 5th and 10th index. To get the whole incident number you need (5,15)

var res = gr.getValue('short_description').substring(5,15);

If the incident number might be in different positions in the short description in future you can use indexOf() to find it.

var index = gr.short_description.indexOf('INC');
var res = gr.getValue('short_description').substring(index, index+10);

If your incident numbers might come with different lengths then it is recommended you use Ankur's suggestion of a regex, is more flexible.