Arrays
// Single Array - Push single values into an array.
// If there is a field which doenst have a single value but comma seperated values then split it and adds it to the array
var x = 0;
var st = '';
var ar = new Array();
var gr = new GlideRecord('cmdb_ci_service');
gr.addEncodedQuery("u_search_tagsISNOTEMPTY");
gr.query();
while (gr.next()) {
x++;
// gs.print(x + " " + gr.u_search_tags); // e.g. Azure, Joe, Harry, OR Azure
st = gr.u_search_tags;
st = st.replace(/,\s*$/, ""); // Remove the last comma and any spaces if there are any
// Does it have a comma
if (st.indexOf(',') != -1) {
var sts = st.split(',');
for (var i = 0; i < sts.length; i++) {
//gs.print("---- " + x + " --" + i + " --" + sts.length + " " + sts[i]); e..g
ar.push(sts[i]);
if (sts.length - 1 != i) {
x++;
}
}
gs.print("----We have a split");
} else {
ar.push(st);
}
}
gs.print("------------------------");
x = 0;
gs.print("ar.length: " + ar.length);
// Print out what is in the array
while (x < ar.length) {
//gs.print("JOSE");
gs.print(x + " " + ar[x]);
x++;
}
gs.print("------------------------");
gs.print("DONE " + x);
// Multidimensional Array. See the bold in the code below in RED
var x = 0;
var st = '';
var ar = new Array();
var gr = new GlideRecord('cmdb_ci_service');
gr.addEncodedQuery("u_search_tagsISNOTEMPTY");
gr.query();
while (gr.next()) {
x++;
// gs.print(x + " " + gr.u_search_tags);
st = gr.u_search_tags;
st = st.replace(/,\s*$/, "");
// Does it have a comma
if (st.indexOf(',') != -1) {
var sts = st.split(','); // Theres more one Search tag
for (var i = 0; i < sts.length; i++) {
var arr1 = [sts[i], gr.support_group.toString(), gr.parent.toString(), gr.sys_id];
//gs.print("---- " + x + " --" + i + " --" + sts.length + " " + sts[i] + "--"+ gr.sys_id);
ar.push(arr1);
if (sts.length - 1 != i) {
x++;
}
}
// gs.print("----We have a split");
} else {
var arr2 = [st.toString(), gr.support_group.toString(), gr.parent.toString(), gr.sys_id];
//gs.print (x+ " " + st + " Sup:" + gr.support_group + " BS:" + gr.parent + " SC:" + gr.sys_id);
ar.push(arr2);
}
}
gs.print("----------XXXXXXX Print out the Array-------------- Array length: " + ar.length);
x = 0;
// Print out the array
for (var a=0; a < ar.length; a++){
gs.print("-- " + a + " " + ar[a][0] + " " + ar[a][1] + " " + ar[a][2]+ " " + ar[a][3]);
}
gs.print("-----------END-------------");
gs.print("DONE " + x);
Comments
Post a Comment