Tap Forms – Organizer Database App for Mac, iPhone, and iPad › Forums › Script Talk › Autocreate day entry based on log entry
- This topic has 0 replies, 1 voice, and was last updated 5 years, 2 months ago by Sam Moffatt.
-
AuthorPosts
-
October 16, 2019 at 9:03 PM #37182
Sam MoffattParticipantOn the main forum there was a posting about having a form for tracking health symptoms. I like Brendan’s idea but I also like to automate things and this one got me curious about using a Link to Form “JOIN” field instead. Whilst Tap Forms won’t let you run a script at record creation, you can create a script field attached to all of the fields in the form and use that to do some work.
I’m attaching a template that has two forms in it: a Days form and a Log Entry form. The Days form has your “Days” in it and you can create a new day entry. There is a “Link to Form” field that links to the “Log Entry” form based on the “Date” fields. The date in the log entry record is “joined” to the date in the days record. If you create a new “Days” record, it will automatically set the date and from there if you add a new “Log Entry” it will create a new “log entry” with today’s date and the current time in it. Date and time are separate to ensure that the linking works automatically. So far though this isn’t any different than Brendan’s original “Link to Form” suggestion, it’s the same sort of flow.
What is different about this is that if you create a new “log entry” record and make some changes to it, it will detect there isn’t a “day” record for itself and create one for you. I gave this a spin on my phone and it seems to work well and on the desktop it mostly works though sometimes I need to click the little “recalculate formulas” button if it doesn’t work.
The script to make this work looks like this:
var date = record.getFieldValue('fld-a415590fcdb649509e193c4eb473a966'); var time = record.getFieldValue('fld-61ed0a92574d4fbc86448ce57a1d46bb'); var symptoms = record.getFieldValue('fld-5c830b5a2499490d93726463d559d68d'); var medications = record.getFieldValue('fld-233b18acd47645ad83c4721bb6ddb7b7'); var pain_level = record.getFieldValue('fld-9221dc10702b40b498bc81cd6b96b907'); var treatments = record.getFieldValue('fld-e31b6235845444b19dda6fe0acd8c2b3'); var days_auto_link = record.getFieldValue('fld-a5fa3403ca284c809e353fc868b7b33d'); var create = true; if (days_auto_link.length == 0 && date) { var days = document.getFormNamed('Days').getRecords(); for(day in days) { var targetDate = days[day].getFieldValue('fld-fca7d2d982924a268353e8d24f8474c7'); if (!targetDate) { console.log('Target date is invalid, skipping.'); continue; } if (date.getTime() == targetDate.getTime()) { console.log('Date equals target date, created already!'); create = false; document.saveAllChanges(); break; } else { console.log(<code>Date comparison failed: ${date} != ${targetDate}</code>); } } if (create) { console.log('Adding new form entry for today'); var newRecord = document.getFormNamed('Days').addNewRecord(); newRecord.setFieldValue('fld-fca7d2d982924a268353e8d24f8474c7', date); document.saveAllChanges(); } } else { console.log('Autolink already exists. Tap Form users all shout hurray because in the JOIN we saved the day!'); }
The first stanza gets all of the fields in the form. This is necessary to ensure that Tap Forms triggers the script to do something as soon as something changes. There isn’t a way to hook into post-create with say a form script so this is the next best thing.
It then sets a
create
flag to true to tell the system to create a new record, it checks to see if theJOIN
field hasn’t already been set and if this record’s date field is unset (it should be autoset but it could be manually deleted). It falls into an else statement that assumes the date is correct and the form is linked with a fun little console message. Tap Forms is good about setting up the link as soon as the record is created in my limited testing so once a “Days” record exists already for the current day, it’ll autolink and save us a bunch of work.Assuming no link exists, the next step is to get all of the records in the
Days
form and iterate over them to look for a candidate date. This is probably overkill because we can probably rely upon Tap Forms validating this for us however I don’t mind being a little defensive here to validate if a record exists.In the loop we check to see if the date in the “Days” record is valid and if it isn’t, it
continue
s to the next iteration of the loop. It then checks to see if it matches our “Date” field and if it does it disable the create flag andbreak
s the loop because we shouldn’t expect any other matches. There are some more console logs printed for validation because it never hurts.Once we exit the loop, we check the
create
variable to see if we should create a new record. If we need to create a new record, it creates a log entry, creates a new record and then sets the record’s date field to match this records date field and saves the changes. Originally I had this set up to just add the record and let the default value from the “Days” form set it automatically but then I realised this wouldn’t work if the record’s date was different to the current date.Attachments:
You must be logged in to view attached files. -
AuthorPosts
You must be logged in to reply to this topic.