Tap Forms Database Pro for Mac, iPhone, iPad and Apple Watch › Forums › Script Talk › Prompt shows + how to auto-run when 3 fields are filled?
- This topic has 9 replies, 3 voices, and was last updated 2 days, 23 hours ago by
Brendan.
-
AuthorPosts
-
September 1, 2025 at 3:00 PM #52911
Mark MoranParticipantHi all—
I’ve got a Tap Forms JavaScript that works as follows:
• From the Coins form, it reads three fields: Coin Type, Year, Mintmark.
• If there’s a match, it looks up matching records in Coin Mintages (there can be multiple) and collects their Variety values.
• It then presents those Variety options in a prompt so the user can pick one, and the choice is written back to the Variety field on Coins.Two issues I’m trying to solve:
1. Prompt includes <unspecified>
• The choice list always includes an extra <unspecified> entry even though I only push actual Variety strings.
• What’s the correct way to prevent that entry from appearing? (e.g., do I need to filter out null/undefined/”” before calling the prompt function, or is there an option/flag in the Tap Forms prompt API that disables an empty choice?)
2. Auto-run when 3 fields are completed
• I’d like this to run automatically whenever Coin Type, Year, and Mintmark are all non-empty on a Coins record—so users don’t have to open the Scripts menu.
• Is there a supported way to trigger a Form Script on field change/record save?
• If not, would a Script field (or a calculated/auto script field) be the right pattern to re-evaluate and prompt as soon as those three fields are populated?======
// Coins → Form Script (current record only; clears Variety on <unspecified>)
// === CONFIG ===
const COINS_FORM_NAME = ‘Coins’;
const MINTAGE_FORM_NAME = ‘Coin Mintage’;const COINS_FIELDS = {
coinType: ‘fld-1231556e0f234de9b2d85f50225b8135’,
year: ‘fld-4b84b047badd4d4cbb4a67ef6839522f’,
mintMark: ‘fld-adb0be16f1a347dbbecc4ef562d779a7’,
variety: ‘fld-2a73d81d137e4f718378819f5e4d54e0’
};const MINTAGE_FIELDS = {
coinType: ‘fld-fdf93db206d141bd956d8c9e7634f25e’,
year: ‘fld-9579d7c74e88414daf27ccbc5d4e0346’,
mintMark: ‘fld-8d72d0971ed34c2c9de6c03d240e9543’,
variety: ‘fld-f76a6eb7ed0c4ae28b77fc6fdf055d53’
};// === Script ===
(function () {
if (typeof record === ‘undefined’ || !record) {
Utils.alertWithMessage(‘No Record’, ‘Run this from a Coins record.’);
return;
}function norm(v) { return (v === null || v === undefined) ? ” : String(v).trim(); }
function eq(a, b) { return norm(a) === norm(b); }var coinType = norm(record.getFieldValue(COINS_FIELDS.coinType));
var year = norm(record.getFieldValue(COINS_FIELDS.year));
var mintMark = norm(record.getFieldValue(COINS_FIELDS.mintMark));if (!coinType || !year) {
Utils.alertWithMessage(‘Missing Data’, ‘Current record needs Coin Type and Year.’);
return;
}var mintageForm = document.getFormNamed(MINTAGE_FORM_NAME);
if (!mintageForm) {
Utils.alertWithMessage(‘Form Not Found’, ‘Could not find form: ‘ + MINTAGE_FORM_NAME);
return;
}var mintageRecords = mintageForm.fetchRecords();
if (!mintageRecords || mintageRecords.length === 0) {
Utils.alertWithMessage(‘No Varieties’, ‘No records in Coin Mintage.’);
return;
}var choices = [];
var seen = {};for (var i = 0; i < mintageRecords.length; i++) {
var r = mintageRecords;
var t = norm(r.getFieldValue(MINTAGE_FIELDS.coinType));
var y = norm(r.getFieldValue(MINTAGE_FIELDS.year));
var m = norm(r.getFieldValue(MINTAGE_FIELDS.mintMark));
var v = norm(r.getFieldValue(MINTAGE_FIELDS.variety));
if (!v) continue;if (eq(t, coinType) && eq(y, year) && eq(m, mintMark)) {
if (!seen[v]) { seen[v] = true; choices.push(v); }
}
}if (choices.length === 0) {
Utils.alertWithMessage(‘No Varieties’, ‘No matching Variety entries found in Coin Mintage.’);
return;
}choices.sort(function (a, b) { if (a < b) return -1; if (a > b) return 1; return 0; });
var title = ‘Select Variety — ‘ + coinType + ‘ ‘ + year + (mintMark ? ‘ ‘ + mintMark : ”);
var prompter = Prompter.new();
prompter.cancelButtonTitle = ‘Skip’;
prompter.continueButtonTitle = ‘Set Variety’;
prompter.addParameter(‘Variety:’, ‘choice’, ‘popup’, choices)
.show(title, function (continued) {
if (!continued) return;// If user leaves the popup on <unspecified> (choice is falsy), clear the field.
if (!choice) {
record.setFieldValue(COINS_FIELDS.variety, ”);
document.saveAllChanges();
return;
}record.setFieldValue(COINS_FIELDS.variety, String(choice));
document.saveAllChanges();
});
})();September 3, 2025 at 12:04 PM #52918
Daniel LeuParticipant1) Maybe it’s because you don’t use a default value. E.g.,
prompter.addParameter('Variety:', 'choice', 'popup', choices, choices[0])
2) Instead of a form script, use a field script. They are automatically triggered when you use
thisRecord.getFieldValue('fld-xxx')
. You could call your form script form a field script as well after checking that your three fields are set.Cheers, Daniel
---
See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricksSeptember 4, 2025 at 3:35 AM #52926
Mark MoranParticipantThanks Daniel. Extremely helpful as usual.
Totally missed the default value. Still has “unspecified” as an option. But at least it now defaults to possible answer I would want.
Using the field script… I’m not clear on field scripts, they are triggered when exactly? Are field scripts triggered when another script uses thisRecord.getFieldVlaue(‘fld-xxx’) as I think you’re saying above? Kind of a chicken and egg thing, where is the thisRecord.getFieldValue used that it triggers the field script or the form script?
September 4, 2025 at 8:49 AM #52928
Daniel LeuParticipantre “unspecified”: To start with, I would check that the list of values really only contains valid values and nothing else. Add a
console.log(choises.join(" | "))
before the prompter code.Re “field script trigger”. This is from an older post from Brendan:
… my Objective-C code scans your source code to determine which fields you’re referencing in the script by looking for the text .getFieldValue(‘fld-…..’). Or when using a variable instead of (‘fld-….’).
When one of the referenced fields changes, the field script is triggered.
Hope this helps!
Cheers, Daniel
---
See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricksSeptember 4, 2025 at 11:06 AM #52932
Mark MoranParticipantThanks again. Knowing how the field script trigger works helps.
The choices are only valid values and nothing else. Still no clue where the unspecified is coming from.
Mark
September 10, 2025 at 4:03 PM #52949
Mark MoranParticipantAgain thanks for your help.
My research says that I can use the prompter in a field script. Also, I can’t run a form script from a field script. This eliminates the process I want to accomplish. I want it to prompt for the variety field from the records in the coin mintage form.
September 10, 2025 at 6:48 PM #52952
Daniel LeuParticipantYou definitely can run a form or document script from a field script. I do this all the time. For a form script, you can use
document.getFormNamed('demo').runScriptNamed('test');
, for a document script usedocument.runScriptNamed('test');
.Do you have a test file you can share? You can email it if you prefer.
Cheers, Daniel
---
See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricksSeptember 11, 2025 at 5:43 AM #52958
Mark MoranParticipantI’ll work on it. I’m learning as I’m going and fixing this is part of the education. I really appreciate your help. But here’s an empty copy of the database for anyone who wants to play with it. I erased the records of every form except the ones in the Data area. It’s a coin collection database, and the Data area contains information common to anyone collecting, eg, mintages, coin types, etc. It’s a work in progress, but anyone is free to use it.
I’m open to anyone’s suggestions for improvements too….
September 11, 2025 at 10:37 PM #52963
Daniel LeuParticipantSecond time I’m trying to post an answer…. the first time caused the forum to crash :-(. So this answer will be a bit shorter.
The field script has an error. The dash
-
in the field ids is not a dash and therefore, no value is read. I modified the script to call the form script in the Coins form:(function() { const ct = record.getFieldValue('fld-1231556e0f234de9b2d85f50225b8135'); const yr = record.getFieldValue('fld-4b84b047badd4d4cbb4a67ef6839522f'); const mm = record.getFieldValue('fld-adb0be16f1a347dbbecc4ef562d779a7'); if (ct && yr && mm) { console.log("condition met") document.getFormNamed('Coins').runScriptNamed('Variety Prompt'); } return ''; })();
When running the Variety Prompt script, I run into an error where the constants were already defined. This has to do with how javascript is handled within Tap Forms Pro. So I moved that section inside your function:
// === Script === (function () { // === CONFIG (your IDs kept as-is) === const COINS_FORM_NAME = 'Coins'; const MINTAGE_FORM_NAME = 'Coin Mintage'; const COINS_FIELDS = { coinType: 'fld-1231556e0f234de9b2d85f50225b8135', year: 'fld-4b84b047badd4d4cbb4a67ef6839522f', mintMark: 'fld-adb0be16f1a347dbbecc4ef562d779a7', variety: 'fld-2a73d81d137e4f718378819f5e4d54e0' }; const MINTAGE_FIELDS = { coinType: 'fld-fdf93db206d141bd956d8c9e7634f25e', year: 'fld-9579d7c74e88414daf27ccbc5d4e0346', mintMark: 'fld-8d72d0971ed34c2c9de6c03d240e9543', variety: 'fld-f76a6eb7ed0c4ae28b77fc6fdf055d53' }; if (typeof record === 'undefined' || !record) { ...
Now it seems to work. Good luck!
Cheers, Daniel
---
See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricksSeptember 11, 2025 at 11:46 PM #52964
BrendanKeymasterSorry for the forum crash. Ran out of disk space on the VM.
-
AuthorPosts
You must be logged in to reply to this topic.