- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-16-2017 01:04 PM
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]);
}
Solved! Go to Solution.
- Labels:
-
Service Catalog
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-16-2017 01:47 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-16-2017 01:47 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-17-2017 05:16 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-23-2018 09:50 AM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-14-2022 07:38 AM
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.
Could you please assist me in getting total number of lines irrespective of complete or partial lines.
Thanks & Regards,
Savita