Tap Forms – Organizer Database App for Mac, iPhone, and iPad › Forums › Script Talk › New Child Record Generated but not Showing
Tagged: addNewRecordToField, Link to
- This topic has 2 replies, 2 voices, and was last updated 2 years, 10 months ago by Mark Kearney.
-
AuthorPosts
-
February 6, 2022 at 7:09 AM #46610
Mark KearneyParticipantHello. Basic programmer here (literally, my understanding comes from my days with BASIC).
I am trying to create a basketball season practice planner where clicking a checkbox for a practice topic automatically adds a record to a “Link To” field (joined by date field). The details for the parent topic can be stored in the child record. The practice topic from the parent form is then stored in array (foci) along with the other topics for that practice. If the checkbox is unchecked, the records in the child form are deleted for a given date and that topic is removed from the array.
This portion of the script (located in a field in the parent form) appears to work but the new child record does not appear in the linked field until records are refreshed. NB: There is a script in the child form that converts the date to text (as with var date_text below) for the search piece.
I would like to get the child record to show in the “Link to” field without needing to refresh. Any help would be greatly appreciated.
function Focus_Script() {
/* variables for Practice Plan */
var practice_date = record.getFieldValue('fld-id'); //practice date in parent form
var OT = document.getFormNamed('Offensive Transition'); // child form;
var offensive_transition = record.getFieldValue('fld-id'); // watches checkbox in parent form;
var date_text = JSON.stringify(practice_date);
let foci=[];/* variables from Offensive Transition */
var OT_date_id = 'fld-id'; // date field from child record;
if (offensive_transition == true) {
/* Search Offensive Transition for existing date and add record if not there */
var records = OT.getRecordsForSearchTerm(date_text);
if (records.length !== 0) {
console.log("DATE EXISTS ALREADY"); // true condition;} else {
foci.push("Offensive Transition"); // false condition;
var new_OT_record = record.addNewRecordToField('fld-id');
new_OT_record.setFieldValue(OT_date_id,practice_date);
console.log("DATE DOES NOT EXIST");
Utils.alertWithMessage('Done.', practice_date + " added."); // false condition;}
} else {/* Delete existing record for a given date in Offensive Transition */
var OT_records = OT.getRecords();
var field_id = 'fld-id'; /date field converted to text;
for (var index = 0, count = OT_records.length; index < count; index++){
var aRecord = OT_records[index];
var field_value = aRecord.getFieldValue(field_id);
if (field_value == date_text) {
OT.deleteRecord(aRecord);
}
}
} // false condition;console.log("EOJ");
return foci;
document.saveAllChanges()Focus_Script();
February 6, 2022 at 10:06 AM #46611
Sam MoffattParticipantThe
document.saveAllChanges()
is after thereturn
statement which means it never gets executed as thereturn
statement returns control to the caller. It also looks like you’re missing a brace from the example code so I’m guessing that should have been before the function call. You can wrap the code in a back tick at the start and end to put it into code mode (or use the HTML tag):function Focus_Script() { /* variables for Practice Plan */ var practice_date = record.getFieldValue('fld-id'); //practice date in parent form var OT = document.getFormNamed('Offensive Transition'); // child form; var offensive_transition = record.getFieldValue('fld-id'); // watches checkbox in parent form; var date_text = JSON.stringify(practice_date); let foci = []; /* variables from Offensive Transition */ var OT_date_id = 'fld-id'; // date field from child record; if (offensive_transition == true) { /* Search Offensive Transition for existing date and add record if not there */ var records = OT.getRecordsForSearchTerm(date_text); if (records.length !== 0) { console.log("DATE EXISTS ALREADY"); // true condition; } else { foci.push("Offensive Transition"); // false condition; var new_OT_record = record.addNewRecordToField('fld-id'); new_OT_record.setFieldValue(OT_date_id, practice_date); console.log("DATE DOES NOT EXIST"); Utils.alertWithMessage('Done.', practice_date + " added."); // false condition; } } else { /* Delete existing record for a given date in Offensive Transition */ var OT_records = OT.getRecords(); var field_id = 'fld-id'; /date field converted to text; for (var index = 0, count = OT_records.length; index < count; index++) { var aRecord = OT_records[index]; var field_value = aRecord.getFieldValue(field_id); if (field_value == date_text) { OT.deleteRecord(aRecord); } } } // false condition; document.saveAllChanges() console.log("EOJ"); return foci; } Focus_Script();
February 6, 2022 at 10:22 AM #46612
Mark KearneyParticipantThank you, Sam.
Indeed it was the placement of
document.saveAllChanges()
that was the source of the issue. I had simply missed copying the missing brace into the code section but also thank you for that catch. -
AuthorPosts
You must be logged in to reply to this topic.