Hi John,
Found a script for modifying an XML using the pre-processor and extracted part of it and modified it to replace the ’ with a line break:
var inFile = openTextReader(data.filename);
var outFile = openTextWriter(data.filename+'.tmp');
var edidata = "";
while ((sLine=inFile.readLine())!=null) {
edidata+=sLine.replace(/'/g,"\n");
}
outFile.write(edidata);
inFile.close();
outFile.close();
copyFile(data.filename+'.tmp',data.filename);
I then found an older OL Learn post that deals with breaking a text string down into its elements here and combined that with the multiple conditions step you suggested.
So for the STX line (STX=ANA:1+0000000000001:SOME COMPANY+0000000000002:OTHER COMPANY+) I had to break it up into elements (using the +) and further split it by sub element (:). The first element I was interested in was Supplier GLN:Supplier Name (0000000000001:SOME COMPANY) so that required a JavaScript defined field that split up the entire row by the ‘+’ symbol, split the element I was interested in by the ‘:’ symbol, and returned the first part:
var STXArray = data.extract(1,4000,0,1,"
").split("+");
var supgln = STXArray[1].trim();
var supglnArray = supgln.split(":");
supglnArray[0].trim();
Then to get the second part I just used the element split variable (supglnArray) in a new field:
EDIT: I had to add an IF statement to check if the sub element exists as not all EDI files use it (and you obviously can’t trim on a NULL). I used the IF rather than just removing the trim as in some sections there can be multiple sub elements (or none).
if (supglnArray[1] == null) {
results = "";
} else {
results = supglnArray[1].trim();
}
After that, it was just rinse and repeat for the next element, Recipient GLN:Recipient Name (although I took advantage of the variable that I created in the first field to split the row - ‘STXArray’):
var repgln = STXArray[2].trim();
var repglnArray = repgln.split(":");
repglnArray[0].trim();
So on and so forth.
It’s going to take me an age to completely pull all 50 rows of data to pieces like this but once it’s done I should be able to reuse it on other similar formats. I appreciate my JavaScript is probably poorly written, but as I mentioned above I only starting using it a week ago
My method is long winded and no doubt there is a more efficient way to do this out there, but hopefully this post will help someone else trying to do something similar.
Thank you both for your assistance.