Tap Forms Database Pro for Mac, iPhone, iPad and Apple Watch › Forums › Script Talk › Recordset from Search or Form for Random Record script
- This topic has 6 replies, 2 voices, and was last updated 3 years ago by
Daniel Leu.
-
AuthorPosts
-
May 31, 2022 at 5:22 PM #47413
David SchwaneParticipantI have this script that returns a random record regardless if I’m on a form or a saved search. Works great but I know using try catch this way is not correct. What is a better way to resolve if I’m trying to get a form recordset or search recordset?
Random_Record();
try {
var records = search.getRecords();
}
catch (err) {
var records = form.getRecords();
}
var record = records[Math.floor(Math.random() * records.length)];
form.selectRecord(record);
May 31, 2022 at 8:05 PM #47414
Daniel LeuParticipantI would use something like this:
if (typeof search !== 'undefined'){ var records = search.getRecords(); } else { var records = form.getRecords(); }
This doesn’t address the corner case where
search
exists but doesn’t contain any records, egrecords.length == 0
.I wouldn’t use
record
invar record = ...
since that is a predefined value.Cheers, Daniel
---
See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricksJune 1, 2022 at 9:29 AM #47415
David SchwaneParticipantthank you that works well
June 1, 2022 at 1:16 PM #47419
David SchwaneParticipantSeeing some unexpected behavior upon further review.
If I run the script from the Scripts menu, works fine. But fails from a button on a custom layout.
As a test, I created a new script with a single line of code.
console.log(typeof search);
From Scripts menu, correctly returns object
From button on form, incorrectly returns undefinedThis is on same search, same record, just running the script from menu vs button.
June 1, 2022 at 2:48 PM #47420
Daniel LeuParticipantWhen you launch a script from a button, do you get any records when using
search.getRecords()
?Cheers, Daniel
---
See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricksJune 1, 2022 at 2:58 PM #47421
David SchwaneParticipantno
console.log(typeof search);
var records = search.getRecords();
console.log(records.length)from menu:
6/1/22, 5:53:57 PM / Project / TEST
object
250from button:
6/1/22, 5:53:50 PM / Project / TEST
undefined
TEST: ReferenceError: Can’t find variable: search, line:(null)June 1, 2022 at 6:43 PM #47423
Daniel LeuParticipantLooks like the context the script runs is different. Something Brendan will be able to answer.
Cheers, Daniel
---
See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricks -
AuthorPosts
You must be logged in to reply to this topic.