Ok, so if you’ve got multiple entries then I’d suggest creating a second form for the data and using a link to form field to store that data in or a table field. This allows you to model the multiple entries but link it back to the main record.
This script is a little longer than the last. I’ve put comments throughout it. You’ll need to fill in the blanks for the field ID’s which you can find via the script editor by clicking on the field and hitting ID or by looking at it’s ID in the field details panel.
The way I’ve constructed it is something that could be optimised but I use this technique to verify the object that is constructed looks “right” and then I add in the piece to addNewRecordToField and setFieldValue at the end which actually creates the records.
Here’s the script:
// fieldNames is a list of field names for each line.
const fieldNames = [
'event_date',
'squadron',
'location',
'aircraft',
'pilot',
'damage_type',
'comments'
];
// aircraft details has the fields for the aircraft line.
const aircraftDetails = ['aircraft_type', 'aircraft_sn', 'aircraft_ext'];
// fieldMap maps the friendly field name to the Tap Forms field name.
// these are the fields IDs from the link to form or table field
const fieldMap = {
"event_date": "fld-001",
"squadron": "fld-002",
"damage_location": "fld-003",
"damage_description": "fld-004",
"damage_percentage": "fld-005",
"aircraft_type": "fld-006",
"aircraft_sn": "fld-007",
"aircraft_ext": "fld-008",
"pilot_rank": "fld-009",
"pilot_name": "fld-010",
"damage_type": "fld-011",
"comments": "fld-012"
};
// input is our sample data, in your script:
// let input = record.getFieldValue('fld-notefieldid');
let input = `02.08.1942
12 Squadron
RAF Hawkinge, tire burst on landing (20%)
Lancaster1, SN.ZX334
SqnLdr. Conran
1) RAF Bomber Accidents
1) no comment.
02.08.1942
12 Squadron
RAF Detling, dmage while on sortie (50%)
Lancaster1, SN.ZX334, BA-
SqnLdr. Conran
1) RAF Bomber Damage
1) no comment.
17.08.1943
61 Squadron
Hamburg, Shot down by a nightfighter (100%)
Lancaster1, SN.ZX334, BA-
SqnLdr. Hewitt
1) RAF Bomber Losses
1) no comment.`;
// lineIndex the index of the line we're processing.
let lineIndex = 0;
// parsedData is an object we're going to put the data in temporarily.
let parsedData = {};
// lines is each of the lines
let lines = input.split("\n");
// process each line...
for (let line of lines) {
// trim the line of extra whitespace
line = line.trim();
// ignore empty lines.
if (line.length == 0) {
continue;
}
// handle each line we're working on
switch (lineIndex) {
// these lines pass thru the value 1:1
case 0:
case 1:
case 5:
case 6:
parsedData[fieldNames[lineIndex]] = line;
break;
case 2:
// damage line is field1, field2 (field3) so let's parse that:
let damagePieces = line.match(/([^,]*), (.*) \((.*)\)/);
if (damagePieces && damagePieces.length > 2) {
parsedData["damage_location"] = damagePieces[1];
parsedData["damage_description"] = damagePieces[2];
parsedData["damage_percentage"] = damagePieces[3];
}
break;
case 3:
// aircraft line appears to have two or three entries so we split
let aircraftPieces = line.split(',').map(elem => elem.trim());
// then we work through each piece of the split line
for (let aircraftIndex in aircraftPieces) {
// and put it in the relevant field
parsedData[aircraftDetails[aircraftIndex]] = aircraftPieces[aircraftIndex];
}
break;
case 4:
// pilot appears to be a simple space split field with two values.
let pilotPieces = line.split(' ').map(elem => elem.trim());
parsedData["pilot_rank"] = pilotPieces[0];
parsedData["pilot_name"] = pilotPieces[1];
break;
default:
console.log("Unknown Line");
console.log(lineIndex);
console.log(line);
}
// increment lineIndex to change which line we think we're processing
lineIndex++;
// if lineIndex becomes 7, we're at the end of this stanza
if (lineIndex > 6) {
// reset the lineIndex back to zero
lineIndex = 0;
// log out our parsed object.
console.log(JSON.stringify(parsedData));
// create a new entry in our link to form field
// remove the /* and */ lines after testing
/*
let newEntry = record.addNewRecordToField("fld-linktoformfieldid");
for (let fieldName in parsedData) {
// set the field value of the field to the value we parsed.
newEntry.setFieldValue(fieldMap[fieldName]), parsedData[fieldName]);
}
*/
// reset the parsedData object to empty.
parsedData = {};
}
}
// save all the changes we made!
document.saveAllChanges();
For the sample data, the console log looks something like this:
{"event_date":"02.08.1942","squadron":"12 Squadron","damage_location":"RAF Hawkinge","damage_description":"tire burst on landing","damage_percentage":"20%","aircraft_type":"Lancaster1","aircraft_sn":"SN.ZX334","pilot_rank":"SqnLdr.","pilot_name":"Conran","damage_type":"1) RAF Bomber Accidents","comments":"1) no comment."}
{"event_date":"02.08.1942","squadron":"12 Squadron","damage_location":"RAF Detling","damage_description":"dmage while on sortie","damage_percentage":"50%","aircraft_type":"Lancaster1","aircraft_sn":"SN.ZX334","aircraft_ext":"BA-","pilot_rank":"SqnLdr.","pilot_name":"Conran","damage_type":"1) RAF Bomber Damage","comments":"1) no comment."}
{"event_date":"17.08.1943","squadron":"61 Squadron","damage_location":"Hamburg","damage_description":"Shot down by a nightfighter","damage_percentage":"100%","aircraft_type":"Lancaster1","aircraft_sn":"SN.ZX334","aircraft_ext":"BA-","pilot_rank":"SqnLdr.","pilot_name":"Hewitt","damage_type":"1) RAF Bomber Losses","comments":"1) no comment."}
To get this to work, fill in the field ID’s (e.g. fld- prefixed items) and remove the /* and */ lines to actually create a new field in the link to form field. Might need some testing but you can see roughly what it’s doing.