- 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-17-2017 09:47 AM
It's definitely based on encoding, but it could be source or browser based. There's 3 newline characters: \r, \n, and \r\n. You can use a regex to test for all instances, both should work equally, but the second might be more readable:
str.split(/\r?\n/);
str.split(/(\r\n|\r|\n)/);