- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-17-2020 05:31 AM
im trying to push multiple records values into an json array , but it returns duplicate values.
var child_ci_value=[];
var child_ci_field=[];
var childci = {};
var relci=[];
var rel=new GlideRecord("cmdb_rel_ci");
rel.addQuery("parent","6a78f28f933a31003b4bb095e57ffb8a");
rel.query();
while(rel.next())
{
var childcmdb=new GlideRecord("cmdb_ci");
childcmdb.addQuery("sys_id",rel.child)
childcmdb.query();
if(childcmdb.next())
{
var fields1=childcmdb.getFields();
for (var i1 = 0; i1 < fields1.size(); i1++) {
var glideElement1 = fields1.get(i1);
if (glideElement1.hasValue()) {
child_ci_field.push(glideElement1);
child_ci_value.push(glideElement1.getName());
}
}
for(var i=0;i<child_ci_value.length;i++)
{
childci[child_ci_value[i]] = child_ci_field[i].toString();
}
gs.log(JSON.stringify(childci),"1")
relci.push(childci);
}
}
gs.print(JSON.stringify(relci))
output:-
before pushing into array it gets different records
1st log
1: {"operational_status":"1","sys_updated_on":"2008-10-26 17:16:08","sys_updated_by":"glide.maint","sys_created_on":"2008-10-26 17:16:08","sys_domain":"global","sys_created_by":"glide.maint","sys_domain_path":"/","install_status":"1","name":"PS Apache01","can_print":"false","sys_class_name":"cmdb_ci_web_server","sys_class_path":"/!!/!(/!3/!#","sys_mod_count":"3","monitor":"false","cost_cc":"USD","support_group":"5f6441efc0a8010e0177fcb589156352","unverified":"false","asset":"77c13e8837f3100044e0bfc8bcbe5d2e","fault_count":"0"}
1: {"operational_status":"1","sys_updated_on":"2008-10-26 17:16:23","sys_updated_by":"glide.maint","sys_created_on":"2008-10-26 17:16:23","sys_domain":"global","sys_created_by":"glide.maint","sys_domain_path":"/","install_status":"1","name":"PS Apache03","can_print":"false","sys_class_name":"cmdb_ci_web_server","sys_class_path":"/!!/!(/!3/!#","sys_mod_count":"5","monitor":"false","cost_cc":"USD","support_group":"5f6441efc0a8010e0177fcb589156352","unverified":"false","asset":"f7c13e8837f3100044e0bfc8bcbe5d31","fault_count":"0"}
1: {"operational_status":"1","sys_updated_on":"2008-10-26 18:31:32","sys_updated_by":"glide.maint","sys_created_on":"2008-10-26 18:31:32","sys_domain":"global","sys_created_by":"glide.maint","sys_domain_path":"/","install_status":"1","name":"SAP WEB03","can_print":"false","sys_class_name":"cmdb_ci_web_server","sys_class_path":"/!!/!(/!3/!#","sys_mod_count":"8","monitor":"false","cost_cc":"USD","support_group":"5f6441efc0a8010e0177fcb589156352","unverified":"false","asset":"f7c13e8837f3100044e0bfc8bcbe5d3b","fault_count":"0"}
1: {"operational_status":"1","sys_updated_on":"2011-08-02 11:14:10","sys_updated_by":"admin","sys_created_on":"2010-11-25 10:34:38","sys_domain":"global","sys_created_by":"felix.bait","sys_domain_path":"/","install_status":"1","name":"Java Application Server FLX","can_print":"false","sys_class_name":"cmdb_ci_app_server_java","sys_class_path":"/!!/!(/!)/!2","sys_mod_count":"3","monitor":"false","cost_cc":"USD","support_group":"5f6441efc0a8010e0177fcb589156352","unverified":"false","asset":"f7c13e8837f3100044e0bfc8bcbe5d3b","fault_count":"0","maintenance_schedule":"82b90aa00ad337027ef3faa2690f25a8","location":"108752c8c611227501d4ab0e392ba97f"}
1: {"operational_status":"1","sys_updated_on":"2008-10-26 17:16:15","sys_updated_by":"glide.maint","sys_created_on":"2008-10-26 17:16:15","sys_domain":"global","sys_created_by":"glide.maint","sys_domain_path":"/","install_status":"1","name":"PS Apache02","can_print":"false","sys_class_name":"cmdb_ci_web_server","sys_class_path":"/!!/!(/!3/!#","sys_mod_count":"4","monitor":"false","cost_cc":"USD","support_group":"5f6441efc0a8010e0177fcb589156352","unverified":"false","asset":"bfc13e8837f3100044e0bfc8bcbe5d2f","fault_count":"0","maintenance_schedule":"82b90aa00ad337027ef3faa2690f25a8","location":"108752c8c611227501d4ab0e392ba97f"}
1: {"operational_status":"1","sys_updated_on":"2008-10-26 18:31:20","sys_updated_by":"glide.maint","sys_created_on":"2008-10-26 18:31:20","sys_domain":"global","sys_created_by":"glide.maint","sys_domain_path":"/","install_status":"1","name":"SAP WEB01","can_print":"false","sys_class_name":"cmdb_ci_web_server","sys_class_path":"/!!/!(/!3/!#","sys_mod_count":"6","monitor":"false","cost_cc":"USD","support_group":"5f6441efc0a8010e0177fcb589156352","unverified":"false","asset":"77c13e8837f3100044e0bfc8bcbe5d38","fault_count":"0","maintenance_schedule":"82b90aa00ad337027ef3faa2690f25a8","location":"108752c8c611227501d4ab0e392ba97f"}
1: {"operational_status":"1","sys_updated_on":"2008-10-26 18:31:37","sys_updated_by":"glide.maint","sys_created_on":"2008-10-26 18:31:37","sys_domain":"global","sys_created_by":"glide.maint","sys_domain_path":"/","install_status":"1","name":"SAP WEB04","can_print":"false","sys_class_name":"cmdb_ci_web_server","sys_class_path":"/!!/!(/!3/!#","sys_mod_count":"9","monitor":"false","cost_cc":"USD","support_group":"5f6441efc0a8010e0177fcb589156352","unverified":"false","asset":"33c13e8837f3100044e0bfc8bcbe5d3d","fault_count":"0","maintenance_schedule":"82b90aa00ad337027ef3faa2690f25a8","location":"108752c8c611227501d4ab0e392ba97f"}
1: {"operational_status":"1","sys_updated_on":"2007-11-10 05:39:12","sys_updated_by":"glide.maint","sys_created_on":"2007-11-10 05:39:12","sys_domain":"global","sys_created_by":"glide.maint","sys_domain_path":"/","install_status":"1","name":"EXCH-SD-07","can_print":"false","sys_class_name":"cmdb_ci_email_server","sys_class_path":"/!!/!(/!3/!7","sys_mod_count":"0","monitor":"false","cost_cc":"USD","support_group":"5f6441efc0a8010e0177fcb589156352","unverified":"false","asset":"33c13e8837f3100044e0bfc8bcbe5d3d","fault_count":"0","maintenance_schedule":"82b90aa00ad337027ef3faa2690f25a8","location":"108752c8c611227501d4ab0e392ba97f","install_date":"2007-11-09 05:38:08"}
once its been pushed into array it gives same records
2nd log
{
"operational_status": "1",
"sys_updated_on": "2007-11-10 05:39:12",
"sys_updated_by": "glide.maint",
"sys_created_on": "2007-11-10 05:39:12",
"sys_domain": "global",
"sys_created_by": "glide.maint",
"sys_domain_path": "/",
"install_status": "1",
"name": "EXCH-SD-07",
"can_print": "false",
"sys_class_name": "cmdb_ci_email_server",
"sys_class_path": "/!!/!(/!3/!7",
"sys_mod_count": "0",
"monitor": "false",
"cost_cc": "USD",
"support_group": "5f6441efc0a8010e0177fcb589156352",
"unverified": "false",
"asset": "33c13e8837f3100044e0bfc8bcbe5d3d",
"fault_count": "0",
"maintenance_schedule": "82b90aa00ad337027ef3faa2690f25a8",
"location": "108752c8c611227501d4ab0e392ba97f",
"install_date": "2007-11-09 05:38:08"
},
{
"operational_status": "1",
"sys_updated_on": "2007-11-10 05:39:12",
"sys_updated_by": "glide.maint",
"sys_created_on": "2007-11-10 05:39:12",
"sys_domain": "global",
"sys_created_by": "glide.maint",
"sys_domain_path": "/",
"install_status": "1",
"name": "EXCH-SD-07",
"can_print": "false",
"sys_class_name": "cmdb_ci_email_server",
"sys_class_path": "/!!/!(/!3/!7",
"sys_mod_count": "0",
"monitor": "false",
"cost_cc": "USD",
"support_group": "5f6441efc0a8010e0177fcb589156352",
"unverified": "false",
"asset": "33c13e8837f3100044e0bfc8bcbe5d3d",
"fault_count": "0",
"maintenance_schedule": "82b90aa00ad337027ef3faa2690f25a8",
"location": "108752c8c611227501d4ab0e392ba97f",
"install_date": "2007-11-09 05:38:08"
},
{
"operational_status": "1",
"sys_updated_on": "2007-11-10 05:39:12",
"sys_updated_by": "glide.maint",
"sys_created_on": "2007-11-10 05:39:12",
"sys_domain": "global",
"sys_created_by": "glide.maint",
"sys_domain_path": "/",
"install_status": "1",
"name": "EXCH-SD-07",
"can_print": "false",
"sys_class_name": "cmdb_ci_email_server",
"sys_class_path": "/!!/!(/!3/!7",
"sys_mod_count": "0",
"monitor": "false",
"cost_cc": "USD",
"support_group": "5f6441efc0a8010e0177fcb589156352",
"unverified": "false",
"asset": "33c13e8837f3100044e0bfc8bcbe5d3d",
"fault_count": "0",
"maintenance_schedule": "82b90aa00ad337027ef3faa2690f25a8",
"location": "108752c8c611227501d4ab0e392ba97f",
"install_date": "2007-11-09 05:38:08"
},
{
"operational_status": "1",
"sys_updated_on": "2007-11-10 05:39:12",
"sys_updated_by": "glide.maint",
"sys_created_on": "2007-11-10 05:39:12",
"sys_domain": "global",
"sys_created_by": "glide.maint",
"sys_domain_path": "/",
"install_status": "1",
"name": "EXCH-SD-07",
"can_print": "false",
"sys_class_name": "cmdb_ci_email_server",
"sys_class_path": "/!!/!(/!3/!7",
"sys_mod_count": "0",
"monitor": "false",
"cost_cc": "USD",
"support_group": "5f6441efc0a8010e0177fcb589156352",
"unverified": "false",
"asset": "33c13e8837f3100044e0bfc8bcbe5d3d",
"fault_count": "0",
"maintenance_schedule": "82b90aa00ad337027ef3faa2690f25a8",
"location": "108752c8c611227501d4ab0e392ba97f",
"install_date": "2007-11-09 05:38:08"
},
{
"operational_status": "1",
"sys_updated_on": "2007-11-10 05:39:12",
"sys_updated_by": "glide.maint",
"sys_created_on": "2007-11-10 05:39:12",
"sys_domain": "global",
"sys_created_by": "glide.maint",
"sys_domain_path": "/",
"install_status": "1",
"name": "EXCH-SD-07",
"can_print": "false",
"sys_class_name": "cmdb_ci_email_server",
"sys_class_path": "/!!/!(/!3/!7",
"sys_mod_count": "0",
"monitor": "false",
"cost_cc": "USD",
"support_group": "5f6441efc0a8010e0177fcb589156352",
"unverified": "false",
"asset": "33c13e8837f3100044e0bfc8bcbe5d3d",
"fault_count": "0",
"maintenance_schedule": "82b90aa00ad337027ef3faa2690f25a8",
"location": "108752c8c611227501d4ab0e392ba97f",
"install_date": "2007-11-09 05:38:08"
},
{
"operational_status": "1",
"sys_updated_on": "2007-11-10 05:39:12",
"sys_updated_by": "glide.maint",
"sys_created_on": "2007-11-10 05:39:12",
"sys_domain": "global",
"sys_created_by": "glide.maint",
"sys_domain_path": "/",
"install_status": "1",
"name": "EXCH-SD-07",
"can_print": "false",
"sys_class_name": "cmdb_ci_email_server",
"sys_class_path": "/!!/!(/!3/!7",
"sys_mod_count": "0",
"monitor": "false",
"cost_cc": "USD",
"support_group": "5f6441efc0a8010e0177fcb589156352",
"unverified": "false",
"asset": "33c13e8837f3100044e0bfc8bcbe5d3d",
"fault_count": "0",
"maintenance_schedule": "82b90aa00ad337027ef3faa2690f25a8",
"location": "108752c8c611227501d4ab0e392ba97f",
"install_date": "2007-11-09 05:38:08"
},
{
"operational_status": "1",
"sys_updated_on": "2007-11-10 05:39:12",
"sys_updated_by": "glide.maint",
"sys_created_on": "2007-11-10 05:39:12",
"sys_domain": "global",
"sys_created_by": "glide.maint",
"sys_domain_path": "/",
"install_status": "1",
"name": "EXCH-SD-07",
"can_print": "false",
"sys_class_name": "cmdb_ci_email_server",
"sys_class_path": "/!!/!(/!3/!7",
"sys_mod_count": "0",
"monitor": "false",
"cost_cc": "USD",
"support_group": "5f6441efc0a8010e0177fcb589156352",
"unverified": "false",
"asset": "33c13e8837f3100044e0bfc8bcbe5d3d",
"fault_count": "0",
"maintenance_schedule": "82b90aa00ad337027ef3faa2690f25a8",
"location": "108752c8c611227501d4ab0e392ba97f",
"install_date": "2007-11-09 05:38:08"
},
{
"operational_status": "1",
"sys_updated_on": "2007-11-10 05:39:12",
"sys_updated_by": "glide.maint",
"sys_created_on": "2007-11-10 05:39:12",
"sys_domain": "global",
"sys_created_by": "glide.maint",
"sys_domain_path": "/",
"install_status": "1",
"name": "EXCH-SD-07",
"can_print": "false",
"sys_class_name": "cmdb_ci_email_server",
"sys_class_path": "/!!/!(/!3/!7",
"sys_mod_count": "0",
"monitor": "false",
"cost_cc": "USD",
"support_group": "5f6441efc0a8010e0177fcb589156352",
"unverified": "false",
"asset": "33c13e8837f3100044e0bfc8bcbe5d3d",
"fault_count": "0",
"maintenance_schedule": "82b90aa00ad337027ef3faa2690f25a8",
"location": "108752c8c611227501d4ab0e392ba97f",
"install_date": "2007-11-09 05:38:08"
}
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-17-2020 06:11 AM
It's because you are pushing a reference to the same object. You are re-populating the same object with the values on each iteration. That is why you see "duplicate" elements as that is where all the references, refer to. You will have to create a separate object literal in each iteration.
If you add: var childci = {}; before the second for and remove it from before the while:
var childci = {};
for(var i=0;i<child_ci_value.length;i++) {
childci[child_ci_value[i]] = child_ci_field[i].toString();
}
You should have an unique object on each push.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-17-2020 06:11 AM
It's because you are pushing a reference to the same object. You are re-populating the same object with the values on each iteration. That is why you see "duplicate" elements as that is where all the references, refer to. You will have to create a separate object literal in each iteration.
If you add: var childci = {}; before the second for and remove it from before the while:
var childci = {};
for(var i=0;i<child_ci_value.length;i++) {
childci[child_ci_value[i]] = child_ci_field[i].toString();
}
You should have an unique object on each push.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-17-2020 06:22 AM
try this.
var child_ci_value = [];
var child_ci_field = [];
var childci = {};
var relci = [];
var rel = new GlideRecord("cmdb_rel_ci");
rel.addQuery("parent", "6a78f28f933a31003b4bb095e57ffb8a");
rel.query();
while (rel.next()) {
var childcmdb = new GlideRecord("cmdb_ci");
childcmdb.addQuery("sys_id", rel.child)
childcmdb.query();
if (childcmdb.next()) {
var fields1 = childcmdb.getFields();
for (var i1 = 0; i1 < fields1.size(); i1++) {
var glideElement1 = fields1.get(i1);
if (glideElement1.hasValue()) {
child_ci_field.push(glideElement1);
child_ci_value.push(glideElement1.getName());
}
}
for (var i = 0; i < child_ci_value.length; i++) {
childci[child_ci_value[i]] = child_ci_field[i].toString();
gs.log(JSON.stringify(childci),"1")
relci.push(childci);
}
}
}
gs.print(JSON.stringify(relci))
Muhammad