Trying to read string values into an array and substring each value

tomoldovan1
Giga Expert

Trying to take a string field that accepts multiple values, push into an array, and get the first 10 characters of each value and write the values back to the field.  Only getting the the first 10 characters of the entire string not the first 10 of each number in the array.

goal is to limit input to 10 characters for each string value.  This has been in place for some time as a string field (4,000) and wanted to see if I could come up with a way without changing the field type.

something like(testing with background script)

var gr = new GlideRecord('change_request');
gr.addQuery("number",'CHG00001');
gr.query();
var nbr = [];
var total = "";
while (gr.next()) {
nbr.push(gr.field_name_nbr);
  for (var i = 0; i < nbr.length; i++) {

          total +=nbr[i].substr(0,10);
           }
}


gs.print(total);

6 REPLIES 6

Mike Patel
Tera Sage

change below and try

nbr.push(gr.field_name_nbr.toString());
and move below outside of while loop

for (var i = 0; i < nbr.length; i++) {

          total +=nbr[i].substr(0,10); 
}

so it will be like

while (gr.next()) {
nbr.push(gr.field_name_nbr.toString());
}

for (var i = 0; i < nbr.length; i++) {

          total +=nbr[i].substr(0,10); 
}

Still only getting the first value in the array.  If i increase to 0,20 I get first number, second number

 

since you have gr.addQuery("number",'CHG00001'); it will only give you 1 record.

Correct but there are multiple values in the nbr field on CHG00001

Number            CHG00001

nbr                   BK1234567 AZ98761132 KT11200033300012

substr (0,10) only returns BK1234567 

substr (0,20) returns BK1234567, AZ9876113

 would like to see it return first 10 characters of each value in the nbr field

BK1234567 AZ98761132 KT11200033