The array is not taking multiple values in while loop
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-29-2022 12:35 AM
Hi,
I have a requirement where i have to fetch the latest code(format is : AU-NSW-SYD-S1...999) from location table and increment accordingly. But, when i am trying to get the number attached to S and storing in array it is just storing one value and overwrites others. Below is the code:
var gr = new GlideRecord('cmn_location');
gr.addEncodedQuery('u_location_codeSTARTSWITHAU-NSW-SYD-S');
gr.query();
var cnt = 0;
while (gr.next()) {
var loc_name = gr.u_location_code;
var split_name = loc_name.split('-');
gs.log("split" + split_name);
cnt = parseInt(loc_name.replace(/^\D+/g, ''));
gs.log("cnt" + cnt);
var arr = [];
arr.push(cnt);
gs.log("arr" + arr);
}
var highest = Math.max.apply(null, arr);
gs.log("high" + highest);
if (isNaN(highest) == true) {
highest = 1;
} else {
highest = highest + 1;
}
Basically, I have to fetch the highest number attached to S and increment that and generate code. But, in array it is not storing multiple values, it is overwriting everytime and only one value gets stored.
Kinldy, help me in fixing this.
Thanks!
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-29-2022 01:06 AM
Declared array outside and try below code
var arr=[];
var gr = new GlideRecord('cmn_location');
gr.addEncodedQuery('u_location_codeSTARTSWITHAU-NSW-SYD-S');
gr.query();
var cnt = 0;
while (gr.next()) {
var loc_name = gr.u_location_code;
var split_name = loc_name.split('-');
gs.log("split" + split_name);
cnt = parseInt(loc_name.replace(/^\D+/g, ''));
gs.log("cnt" + cnt);
arr.push(cnt);
}
gs.info("arr" + arr); // Check this array values
var highest = Math.max.apply(null, arr);
gs.log("high" + highest);
if (isNaN(highest) == true) {
highest = 1;
} else {
highest = highest + 1;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-29-2022 02:11 AM
Hi Kalyani,
That worked! Just one more help needed.
This code is working fine for the already existing codes i.e incrementing properly. But, if there is new code which does not exist in the table,for example, it should generate AU-NSW-NEWCITY-S1.
But the scenario for non-existing or new codes is not handled in the above code.
Could you please help me in achieving this scenario as well in this code??
Thanks!