Split multi line text box on enter

ncp
Mega Guru

I have a multiline text field in a catalog item that I scan asset serial numbers into. The barcode scanner automatically adds a line feed at the end of it ex:

ABC123

DEF123

and so on

When they checkout, all I do is run a script and close the task. In the script I tried reading that in a into a variable and splitting that based on "\r" and "\n" and I get an array length of undefined. Would anyone know why..my script below:

var assetArray=[];

assetArray = current.variables.multi_scanned_assets;

assetArray = assetArray.split("\n");

gs.log("assetArray length " + assetArray.length);

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

  gs.log("assetArray " + assetArray[i]);

}

1 ACCEPTED SOLUTION

Shiva Thomas
Kilo Sage

Hi Chandran,



You may want to find the exact nature of the invisible escaped character that are in your string.


For this you can use...



gs.log(JSON.stringify(current.variables.multi_scanned_assets.toString()));



Once you found them, your script should work. Use the for the split in line 10.


// Creating a fake current to try your code


var current = new GlideRecord('incident');


current.get('d71f7935c0a8016700802b64c67c11c6');


current.variables.multi_scanned_assets = current.close_notes; // I put your text in the close notes field of an incident



// Finding the escaped char


gs.log(JSON.stringify(current.variables.multi_scanned_assets.toString())); // ABC123\r\nDEF123 ... this could be different in your case



// Your code...


assetArray = current.variables.multi_scanned_assets.toString().split("\r\n"); // \r\n where the escaped characters in my example put yours here.


gs.log("assetArray length " + assetArray.length);


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


  gs.log("assetArray " + assetArray[i]);


}


The toString() on line 7 and 10 are required for the script to work.



Capture d


View solution in original post

5 REPLIES 5

Shiva Thomas
Kilo Sage

Hi Chandran,



You may want to find the exact nature of the invisible escaped character that are in your string.


For this you can use...



gs.log(JSON.stringify(current.variables.multi_scanned_assets.toString()));



Once you found them, your script should work. Use the for the split in line 10.


// Creating a fake current to try your code


var current = new GlideRecord('incident');


current.get('d71f7935c0a8016700802b64c67c11c6');


current.variables.multi_scanned_assets = current.close_notes; // I put your text in the close notes field of an incident



// Finding the escaped char


gs.log(JSON.stringify(current.variables.multi_scanned_assets.toString())); // ABC123\r\nDEF123 ... this could be different in your case



// Your code...


assetArray = current.variables.multi_scanned_assets.toString().split("\r\n"); // \r\n where the escaped characters in my example put yours here.


gs.log("assetArray length " + assetArray.length);


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


  gs.log("assetArray " + assetArray[i]);


}


The toString() on line 7 and 10 are required for the script to work.



Capture d


Hi Shiva,



That seemed to do it! I I have the barcode scanner in the office I will try it from there on Monday and then update this question



Thanks for your help


 

It was really helpful. I'm new on Javascript development (or any kind of web-based development for the matter) but I was hired anyway due to logic and problem solving abilities. It's good to learn about methods like stringify to debug, it helped me solve a problem I was having with my current code.

Thanks!

Hi Shiva,

I am trying to get total number of lines entered in multi line text variable using below script, but I am not getting exact number of lines.

var str=current.variables.variablename.toString().split(/(\r\n|\r|\n)/); 

gs.info(str.length);

Example: I have entered below text in multi line text variable and it has total11 lines, but I am not getting 11 as a result.

find_real_file.png

Could you please assist me in getting total number of lines irrespective of complete or partial lines.

Thanks & Regards,

Savita