Need help with code - For each in array not working

SNDEV27
Tera Contributor

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();
    }
}

 

1 ACCEPTED SOLUTION

Debasis Pati
Tera Guru

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.

View solution in original post

4 REPLIES 4

Debasis Pati
Tera Guru

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.

This worked. Thanks. 
The issue was i was directly using it as array without parsing

Great Happy learning

Ankur Bawiskar
Tera Patron
Tera Patron

@SNDEV27 

share how the JSON looks.

If my response helped please mark it correct and close the thread so that it benefits future readers.

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