Hi Greg,
There’s no function to search a parent form for any values contained in a related form.
However, you can workaround this by using either a Script field or a Calculation field.
On your form B, add a Calculation field. Edit the formula and choose a field from the list of fields that show up that are contained within the parent form A. Any field will do. Then you can add a Saved Search that checks to see if the value of the calculation field is empty or not.
Hope that helps.
Thanks,
Brendan
Brendan,
Thank you very much! Problem solved.
I will hide the script field (as you have suggested), but just to know for the future, will that script field always show a ‘1’ regardless of what is in the other fields?
Brendan,
Thank you for the explanation.
I have tried to follow the approach you outlined. But it appears not to work: the script field and the Time Charge For fields are not updated.
I created:
1. Time spent Field = type: number
2. Time spent to time charged for Field = type: script.
3. Time charged for Field = type: number.
The Time spent to time charged for Field only shows a “1” regardless of what I put in the Time spent Field and the Time charged for Field is not updated.
Attached is the Form. Could you let me know where I have gone wrong?
Attachments:
You must be
logged in to view attached files.
Hi Victor,
Yes. You can do this with a Script field.
In a Script field when you use the record.getFieldValue(field_id) function, Tap Forms begins watching that field for any changes made to it. It executes the script if the value of that field changes.
So by adding a Script field, you could have Tap Forms copy the value from your Time Spent field into your Time Charged field. Now normally a Script field will display its contents in your form, but recently I added a Hide field function to Tap Forms 5.3 which will let you hide any field. So you could have this hidden Script field that’s just watching what’s going on with your form and running automatically when fields the script is watching change.
Thanks!
Brendan
Hi Victor,
You would need to use a Script field instead of a Calculation field to be able to do something like that.
Here’s a sample that might help:
var first_name_id = "fld....";
var last_name_id = "fld...";
var middle_name_id = "fld...";
var firstName = record.getFieldValue(first_name_id);
var lastName = record.getFieldValue(last_name_id);
var middleName = record.getFieldValue(middle_name_id);
var fullName = "";
if (middleName) {
fullName = firstName + " " + middleName + " " + lastName;
} else {
fullName = firstName + " " + lastName;
}
var result = fullName.toUpperCase();
result;
You want a Script field just as you’ve created. Because you’re wanting to display the first passport information in the parent form.
Here’s the exact script you can just copy and paste into your Passport Script:
// declare the field ID for the passport field. Select the field and click the ID button to get the field ID.
var passport_id = 'fld-bb12ade9886146818f0cee47af4a36d5';
var result = '';
// From the currently selected record, get the list of records from the passport field.
var passportRecords = record.getFieldValue(passport_id);
// if we have any passport records, get the passport information.
if (passportRecords.length > 0) {
// Get the very first passport record from the list of passportRecords.
var firstPassportRecord = passportRecords[0];
// declare the passport field ids.
var passport_number_id = 'fld-fc5f7b08aa244466919fd400714ffa6b';
var date_of_expiry_id = 'fld-b73ebd82a8b2479882e9d2fc5b161ff9';
var country_id = 'fld-b502438543564135859a133912994d3f';
// From the first passport record we grabbed above, get the passport number value.
var number = firstPassportRecord.getFieldValue(passport_number_id);
var expiryDate = firstPassportRecord.getFieldValue(date_of_expiry_id);
var country = firstPassportRecord.getFieldValue(country_id);
// and do whatever you want with the values. What you had is almost what you needed:
result = "having a " + country + " passport, with a date of expiry of " + expiryDate.toLocaleDateString() + " and a passport number " + number;
}
result;
Brendan,
Thank you very much for explaining the script, and I can now understand the logic of it but I am having difficulty applying it my database.
As being completely new I am still not clear whether I should be creating
1. a new field of the type script; or
2. a Form script
If a new field script, should I creating a field in the Client Contact form or the Passport form?
Attached is a backed up version of the Forms to show how it actually looks.
Attachments:
You must be
logged in to view attached files.
Brendan,
Thank you for the reply.
As I mentioned Javascript is unknown me (but it is clear I will have to learn it), but in the meantime I wonder if you could provide some explanation for each of the lines of code you provided (to try to understand how to relate the code to the example I gave in my initial post):
var passport_field_id = ‘fld-….’;
var passportRecords = record.getFieldValue(passport_field_id);
if (passportRecords.length > 0) {
var firstPassportRecord = passportRecords[0];
var number_field_id = ‘fld….’;
var number = firstPassportRecord.getFieldValue(number_field_id);
// and do whatever you want with the values/
}
And I in particular how I write the code for
// and do whatever you want with the values/
if I wished to produce:
“having a ” + [country] + ” passport, with a date of expiry of ” + [date of expiry] + ” and a passport number + ” ” [passport number]
Any help you could provide would be very gratefully received.
Yes, it would have to be a Script Field and not a Form Script. That’s because Form Scripts all are run manually whereas a Script Field is run when a value it references changes.
yes, the record.getFieldValue(field_id) reference is what tells Tap Forms that script is referencing that field, so when that field changes its value, run the script that references it.
I’ve changed things for the next update with respect to editing scripts. No matter where you edit a script from in the next update, Tap Forms will go full screen for the editor. So there will be no chance of accidentally switching records and losing your script contents.
It’s best to declare variables within a function declaration.
function runPrompter() {
var upc_lookup_id = 'fld-8b5f99cd185044f8a0b35204db4c86ac';
record.getFieldValue(upc_lookup_id);
var callbackFunction = function() {
console.log(value_1);
};
var value_1;
let prompter = Prompter.new();
prompter.addParameter('Label 1').show('Message prompt', callbackFunction);
record.setFieldValue(upc_lookup_id, false);
document.saveAllChanges();
}
runPrompter();
That should work.
But it may be that you’ve found a bug that’s not clearing out the JSContext when you run the script the second time.
Also probably that I haven’t tested the prompter within a Script Field. I’ve only ever used it within a Form Script.
What is the recommendation in relation to declaration of variables in scripts?
I have a test/demo field script which is executed every time a check mark is changed.
The problem I have is when this is run for the 2nd and subsequent times I get an error since the variable is already declared (see error on screenshot attached).
var upc_lookup_id = 'fld-8b5f99cd185044f8a0b35204db4c86ac';
record.getFieldValue(upc_lookup_id);
var callbackFunction = function() {
console.log(value_1);
};
var value_1;
let prompter = Prompter.new();
prompter.addParameter('Label 1')
.show('Message prompt', callbackFunction);
record.setFieldValue(upc_lookup_id, false);
document.saveAllChanges();
It’s entirely possible I’m doing something daft since I’m new to javascript!
Attachments:
You must be
logged in to view attached files.
Fixed it, I was using a form script not a field script (assume it has to be the latter?).
Also I didn’t have this line in the script which I think is needed?
record.getFieldValue(upc_lookup_id);
Thanks
Just noticed a minor issue I think. If I’m editing a script and don’t save it then change the view on the left hand navigation then there’s no warning and my changes are lost. However if I navigate away using the right hand pane then I do get a warning.
Example in image (green gives warning, red does not).
Attachments:
You must be
logged in to view attached files.
Ah that’s an excellent idea. I tried it and it’s not working though, no-doubt an issue with me.
I have a Form Script with a reference to the check mark field in it:
var upc_lookup_id = ‘fld-8b5f99cd185044f8a0b35204db4c86ac’;
Which I assumed was sufficient to trigger the script when that field is updated?
Assume I’m missing something :-)