Tap Forms – Organizer Database App for Mac, iPhone, and iPad › Forums › Script Talk › Replicate “Select and link existing records” function in a script
- This topic has 3 replies, 2 voices, and was last updated 2 years, 9 months ago by Sam Moffatt.
-
AuthorPosts
-
February 26, 2022 at 4:26 PM #46883
Bruce MeikleParticipantI’, new to Javascript and relatively new to TapForms. I need to replicate the “Select and link existing records” function in a script. In certain circumstances I am creating a new “transaction” (new record) inside a script (using AddNewRecordToField) and once created I need to set the “Link From Field” to an existing “account” record in the script so that the user doesn’t have to take the extra records to go to the subform and select (probably the wrong) “account”, or even worse, not select one at all.
In summary:
1. A new transaction item is created in a script using AddNewRecordToField();
2. I need to update the “Link from field” to select an existing account record inside the script;Is this possible, and if so how would I do it?
Attachments:
You must be logged in to view attached files.February 26, 2022 at 5:30 PM #46886
Sam MoffattParticipantTwo things you’ll want to do: do a
document.saveAllChanges()
after you’ve set the field values and you’ll need to add the record to the link to form field in the parent form.The first one is a quirk I’ve run into sometimes where when creating records and links things get weird. The best workaround for that is to force a save after creating the record but before you link it and then another save afterwards. This might not be always necessary but it’s fixed enough hard to debug situations that I recommend it for your own sanity (easier to remove it later and confirm not needing it than spend half an hour line by line trying to understand why something isn’t behaving the way you expect).
The second is another quirk where you can’t just set the record on the link from form field, you need to do a
addRecordToField
on the link to form field. That’ll look something like this:dr_posting.addRecordToField(new_item, 'fld-drposting_linktoform_id'); cr_posting.addRecordToField(new_item, 'fld-crposting_linktoform_id');
And a save all changes afterwards as well. That should then create the link back to the record you just created. This is a side effect of the way the data model works where the internal link records are defined by their link to form ID rather than the from link form ID.
Hope that makes sense and helps you get this sorted.
February 26, 2022 at 10:42 PM #46887
Bruce MeikleParticipantHi Sam,
Excellent! It does make sense. That achieves exactly what I want to do – in a simpler database. I am still having issues in the more complex example that prompted the question (an additional blank account record is created now), but the simpler model works. I need to spend some debug time on my main database, but now that I now what is involved, hopefully I can solve it.
The saveAllChanges after each record change doesn’t appear to make much difference but I get what it is trying to achieve. It sounds like a good practice to use.
BTW – I have learnt a lot about the JavaScript/TapForms model from your YouTube videos. Very helpful. And thanks for your very quick response to this question!
- This reply was modified 2 years, 10 months ago by Bruce Meikle.
February 27, 2022 at 2:09 PM #46893
Sam MoffattParticipantGood to hear it solves the simpler case, hopefully it comes together for the more complex situation eventually. The
saveAllChanges
is situational, it’s harmless when you don’t need it and useful when you do need it.Great to hear the videos were helpful, I’ve been a bit time poor lately but hopefully I’ll be able to carve out some time to get on top of that again soon to work on my backlog.
-
AuthorPosts
You must be logged in to reply to this topic.