glide record for loop help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-08-2024 08:05 PM
hi All ,
i have a list of incidents stored in a variable ( separator is comma )
var1 = 'inc1 , inc2 , inc3'
In my server side script i need to do a glide on the incident table and query for each incident number , one by one ( first query for inc1 , then query for inc 2 in a loop ).This is a basic scripting question i know but need some hint on how to fetch the comma separated values into the for loop one by one , , so that i can then do a glide query .
thanks a lot in advance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-08-2024 08:26 PM
Hi @Gary22 here is the javascript code to iterate over the incidents, I did not include call to glide.
var1 = 'inc1 , inc2 , inc3'; // input
var var2 = var1.split(',');
var2.forEach ( processInc );
function processInc(inc) {
// process individual incident
gs.info( inc.trim() ); // trim to remove spaces
}
hope this helps
--
Bala Guthy
Please mark this as a solution or helpful as you see fit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-08-2024 08:30 PM
@Gary22 : Please use the below script.
var incidentList = ['INCC0011392', 'INCC0011391', 'INCC0011390'];
incidentList.forEach(callbackFn);
function callbackFn(number) {
gs.log('Incident Number ==' + number);
// I am printing the number one-by-one.
// you can add here your logic
}
Sample Output:
Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-08-2024 08:30 PM
Hi @Gary22 ,
Try below code
// Your comma-separated incidents
var var1 = 'inc1,inc2,inc3';
// Split the values into an array
var incidentsArray = var1.split(',');
// Iterate through the array
for (var i = 0; i < incidentsArray.length; i++) {
// Trim to remove any extra spaces
var incidentNumber = incidentsArray[i].trim();
// Perform Glide query for each incident number
var incidentGR = new GlideRecord('incident');
incidentGR.addQuery('incident_number', incidentNumber);
incidentGR.query();
// Process the result
if (incidentGR.next()) {
// Do something with the incident record
gs.info('Incident ' + incidentNumber + ' found: ' + incidentGR.getValue('short_description'));
} else {
gs.info('Incident ' + incidentNumber + '
not found');
}
}
Thanks,
Danish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-08-2024 08:54 PM
It's an easy one.
Rather than asking the question in community and using the exact answer you should understand how to debug it, this will help you develop your scripting skills as well.
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader