- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2020 10:45 PM
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);
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2020 11:37 PM
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:
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-22-2020 12:48 AM
Glad that my solution worked.
Please remember to mark appropriate responses helpful as well.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-22-2020 12:07 AM
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");
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-22-2020 12:46 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-22-2020 02:01 AM
Ah oaky. When you said 5th position, i thought you meant 5th word.
Then you can go with substring or regex.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-22-2020 12:36 AM
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.