- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2025 06:05 AM
Hello Devs,
I am working on a logic where in i need to use for each in an array. But it is not working. Tried to debug but no luck.
Can you please help me out with this.
var existingAppSrvArrStr = JSON.stringify(existingApp);
if (existingAppSrvArrStr) {
gs.log('final workflow existingApp ' + existingApp);//getting this log
existingAppSrvArrStr.forEach(mapExistingAppSrv);
}
function mapExistingAppSrv(item) {
gs.log('final workflow asset tag -- ' + asset_tag + ' -- name-- ' + item.name); // NO LOG
var grAppSrvDiscovered = new GlideRecord('cmdb_ci_service_discovered');
grAppSrvDiscovered.addEncodedQuery('u_apm_u_number=' + asset_tag + '^name=' + item.name);
grAppSrvDiscovered.query();
if (grAppSrvDiscovered.next()) {
gs.log('final workflow'); //NO LOG
grAppSrvDiscovered.u_web_url = item.url;
grAppSrvDiscovered.support_group = item.supportGroup;
grAppSrvDiscovered.update();
}
}
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2025 06:14 AM
Hello @SNDEV27 ,
Please try the below
// If existingApp is a JSON string, parse it into an array
var existingApp = JSON.parse(existingAppStr); // assuming existingAppStr is a string here
if (Array.isArray(existingApp)) {
existingApp.forEach(mapExistingAppSrv);
} else {
gs.log("existingApp is not an array");
}
function mapExistingAppSrv(item) {
gs.log('final workflow asset tag -- ' + asset_tag + ' -- name-- ' + item.name); // This should work now
var grAppSrvDiscovered = new GlideRecord('cmdb_ci_service_discovered');
grAppSrvDiscovered.addEncodedQuery('u_apm_u_number=' + asset_tag + '^name=' + item.name);
grAppSrvDiscovered.query();
if (grAppSrvDiscovered.next()) {
gs.log('final workflow'); // This should also show up in the logs now
grAppSrvDiscovered.u_web_url = item.url;
grAppSrvDiscovered.support_group = item.supportGroup;
grAppSrvDiscovered.update();
}
}
the existingAppSrvArrStr is a string not an array. The forEach method is an array function, so if you call forEach on a string, it won't work as expected.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2025 06:14 AM
Hello @SNDEV27 ,
Please try the below
// If existingApp is a JSON string, parse it into an array
var existingApp = JSON.parse(existingAppStr); // assuming existingAppStr is a string here
if (Array.isArray(existingApp)) {
existingApp.forEach(mapExistingAppSrv);
} else {
gs.log("existingApp is not an array");
}
function mapExistingAppSrv(item) {
gs.log('final workflow asset tag -- ' + asset_tag + ' -- name-- ' + item.name); // This should work now
var grAppSrvDiscovered = new GlideRecord('cmdb_ci_service_discovered');
grAppSrvDiscovered.addEncodedQuery('u_apm_u_number=' + asset_tag + '^name=' + item.name);
grAppSrvDiscovered.query();
if (grAppSrvDiscovered.next()) {
gs.log('final workflow'); // This should also show up in the logs now
grAppSrvDiscovered.u_web_url = item.url;
grAppSrvDiscovered.support_group = item.supportGroup;
grAppSrvDiscovered.update();
}
}
the existingAppSrvArrStr is a string not an array. The forEach method is an array function, so if you call forEach on a string, it won't work as expected.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2025 06:39 AM
This worked. Thanks.
The issue was i was directly using it as array without parsing

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2025 07:32 AM
Great Happy learning
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2025 06:23 AM
share how the JSON looks.
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