I know there are some methods exposed to the pick list object. I have not got any success using it. I have the iOS version of Tap Forms 5.
I am working on a movie database. I have a need to have a pick list of all actors that are in the current record movie.
I have written a script that gets the actors from an online service. It deletes all rows from a hidden actor form, then populated the form with actors from the movie which is the current record.
The actors Pick List gets it’s values from the Actors Form. The problem is the Pick List seems to only populate with the updated values on document load.
Is there any way of triggering a Pick List to populate progamically? This would solve my problem.
Or am I going about this all wrong?
If there was a way to alter the dictionary object associated with Pick List that would work too. However I have not been able to get a Pick List object to return a dictionary, all I get is an undefined object. I know I am doing something wrong. An example of using the Pick List object and methods would be great.
As @cattailnu said, it’s a short hand conditional known as a ternery operator. I feel it makes things a little more concise instead of having to fit in an if/then/else clause, we can collapse it down to a single line.
If you look at the MDN link the use case we’re referring to is the “handling null values” one where if the variable is undefined (the error you reported), we use the alternate value. If it’s a “truthy” value (basically anything not falsy, aka false, 0, 0.0, an empty string like "", null, NaN or undefined), then we use the same value.
So the reason why it’s repeated is because if the value is truthy, we want to use it but if it is falsy we want to return the 0.0 value as a fallback. There might be some data that this breaks on but so long as it’s an actual number it should do what you need.
It’s saying the value of the variable is undefined and not a valid value. A quick trick would be to rewrite to insert an empty value:
form.runScriptNamed(‘sprintf’);
var s_total_for_invoice = sprintf(“%0.2f”,total_for_invoice ? total_for_invoice : 0.0);
var s_payment_amount = sprintf(“%0.2f”,payment_amount ? payment_amount : 0.0);
console.log(s_total_for_invoice + ” ” + s_payment_amount ? s_payment_amount : 0.0);
It should capture the undefined and turn it into a zero value. That should at least cause the script not to fail.
Looks like I missed @cattailnu’s post, I don’t know why globalThis wouldn’t be defined, it seems to work for me and it should exist as it’s supposed to be a JSC provided variable so it not being set seems odd.
Based on the above, I entered the form script as Sam managed and then in the script where I wish to use sprint I added the following code:
form.runScriptNamed(‘sprintf’);
var s_total_for_invoice = sprintf(“%0.2f”,total_for_invoice);
var s_payment_amount = sprintf(“%0.2f”,payment_amount);
console.log(s_total_for_invoice + ” ” + s_payment_amount);
The field total_for_invoice is a calculation field and payment_amount is a number field.
The first record I ran the script (recalculate formulas) it worked, the second and subsequent times I am getting the following error:
Wording for invoice: TypeError: [sprintf] expecting number but found undefined, line:(null)
However, occasionally it does work and then a further run it will stop working.
Using the example database that T.L. Ford provided has not resulted in the error at all.
Any help would be gratefully received.
Sometimes you can use Google to search the site (e.g. https://www.google.com/search?client=safari&rls=en&q=site:www.tapforms.com/forums+number+formatting&ie=UTF-8&oe=UTF-8) to find stuff.
If you have a simple return value in your script field (e.g. you return 150 or 150.5) if you set the format of the field to number, you can use the Tap Forms field formatting options to change the number of decimal places to be two.
If you need to format in Javascript, I made a port of a Javascript sprintf implementation as a part of the Script Manager stuff or you can directly grab it from GitHub. This adds an sprintf function you can use to format numbers in Javascript as a part of a larger string formatter.
For Brendan,
I have asked this question before and have received an answers, but until now not needed it. However I can see I have asked 49 questions but cannot access any posting beyond the first page. It is likely the answer is on one of the pages I cannot access.
Now the question…
Question
In some of the scripts I have created there are figures / sums taken from fields.
But I would like to format them correctly. For example:
1. if the amount is 150.00 it appears in the script is run as 150
2. if the amount is 150.50 it appears in the script as 150.5
etc.
How is it possible to format numbers so that there are always two decimal places?
Hi,
I’ve been trying to write a java script for some time without success, because my skills are limited (i’m still learning ;-)).
The
My path / goal can be described as follows:
1. A record (“current_record”) is currently selected in form “AR”. A new record (“new_record”) should be created in the form “AR”.
2. The new record (“new_record”) should contain all (or selected) linked form (“AR-Posten”) records (“linkedform_records_of_current_record”) of the currently selected record (“current_record”).
3. AND: Data from only CERTAIN fields should be taken from the linked form records (“linkedform_records_of_current_record”) and filled in the fields of the new child records.
My code (only an exerpt) looks like this:
// linked form AR-Posten id
var ar_posten_id = ‘fld-265897a72a6f467c9185cfeae6c31d57’;
var ar_posten = record.getFieldValue(ar_posten_id);
// field ids from linked form records "AR-Posten", only two for example
var p_id = 'fld-59deac8c932245ca81925170193f62f3';
var quote_id = 'fld-c00267497ef94d70b6778329cff05d0b';
// add new record to AR
var new_ar = form.addNewRecord();
for (var index = 0, count = ar_posten.length; index < count; index++){
var p = ar_posten[index].getFieldValue(p_id);
var quote = ar_posten[index].getFieldValue(quote_id);
...
My question is:
How can i add/link new child records (“AR-Posten”) to the new record (“AR”) and fill each with the field data from “child_records_of_current_record”.
Actually there is no function in the java script API with which one can duplicate records and at the same time all linked child form records. The fields (ids) for the current record and the linked form records (only for the first stage) to be duplicated should be determined in the function.
I hope my explanation is understandable.
Thanks in advance
Chris
Sam,
Thank you very much and for the revised code. At first (other than the first console.log) I was still getting “undefined”.
Then realised that the if statement is the wrong way around for the if part of the if/else:
if (company_name == null)
statement the code that follows should not be for the company for the individual.
After this it works fine.
Thank you again. I will have to explore your Script Manager Form Logger
I think what might be happening is that you’re getting tricked up by Javascript variable scope, it doesn’t feel right but you’re declaring the Dear_Name and Email_to_send_invoice inside the if statement. I would personally probably declare these at the top and maybe put a sentinel value in:
// Return values
var Dear_Name = "Placeholder";
var Email_to_send_invoice = "test@example.com";
This would then go to validate that they’re being overwritten properly. I don’t think that’s your problem here but it’s an approach I’d take as a first pass.
The second pass is rewriting so that the branches are a little more clear. You’re setting up a bunch of variables at the top of the script and then using a smaller if statement at the bottom so it’s hard to know which statements belong to the block. My next step is a rewrite to try to group the variables together:
// Return values
var Dear_Name = "Placeholder";
var Email_to_send_invoice = "test@example.com";
// Form ID
var client_contact_details_id = 'fld-c53e48e48e3a40c7a5656ab92f39ecc9';
var client_first_name_and_email_address_details = record.getFieldValue(client_contact_details_id)
//Company details
// company name
var company_name_id = 'fld-a3300e3db044421598ae171c0a2d4834';
var company_name = client_first_name_and_email_address_details.getFieldValue(company_name_id);
console.log(company_name);
if (company_name == null) {
// first name of company contact
var first_name_company_contact_id = 'fld-24ee4dae33d941bf81e1667853a7a949';
var first_name_company_contact = client_first_name_and_email_address_details.getFieldValue(first_name_company_contact_id);
// email address of company contact
var email_company_contact_id = 'fld-8cfa7613f04f4a8b82652bc27f3c05df';
var email_company = client_first_name_and_email_address_details.getFieldValue(email_company_contact_id);
Dear_Name = first_name_company_contact;
Email_to_send_invoice = email_company;
} else {
//Individual details
// first name of individual
var first_name_id = 'fld-45599a4f0a6342d8973faed53dfbfed8';
var first_name = client_first_name_and_email_address_details.getFieldValue(first_name_id);
// Email address of individual
var email_id = 'fld-8f9a928a7d554527b4127ef811136210';
var email_individual = client_first_name_and_email_address_details.getFieldValue(email_id);
Dear_Name = first_name;
Email_to_send_invoice = email_individual;
}
console.log(Dear_Name + " " + Email_to_send_invoice);
There was some dead code at the top that I eliminated along the way which makes it a little easier to follow.
I put in a console.log(company_name) to see what is in that variable so I would know what pathway to take. You’ve got a couple of the console.log statements but I’d probably beef it up to make sure what you get back from getFieldValue is set to a value.
The last step is to make sure all of the field ID’s make sense. You’re not getting any runtime errors so the client_first_name_and_email_address_details field is set to a correct value but I’d double check the other ID fields. It’s easy to accidentally hook up the wrong fields (in fact I have it on video!) so it never hurts to go back and double check. Keep in mind that Tap Forms won’t give you an error if you give it a field ID that isn’t set in the record, it’ll return undefined. One other trick I have here is that in my Script Manager is a Form Logger that can output variables similar to the Tap Form’s Script Editor but prefixed with the form name as well, e.g.:
// Boxes
var boxes_formID = "frm-8ba92c77b5e8476a987945f40ff2b41a";
var boxes__title_fldID = "fld-e289240daccc40409d75aee26493110c"; // text
var boxes__box_id_fldID = "fld-f25492cf56384369b278f7ea41ee258f"; // text
var boxes__note_fldID = "fld-69754dee7e9247e0b3087e2526881626"; // note
var boxes__dimensions_fldID = "fld-3e5e30f1d352495f989cc0dc1b306128"; // text
var boxes__box_type_fldID = "fld-97b2ab2bdbb1400d9384c7634e7c6d48"; // text
var boxes__box_photos_fldID = "fld-72a520cff50a4bbd92f1e8ff6eec91db"; // photo
var boxes__box_is_full_fldID = "fld-c824d1ff2ced4b948b5907e0a4804bca"; // check_mark
var boxes__purchases_fldID = "fld-482c4f5c71ba4cd7a06c32d1c1d7d983"; // form
var boxes__containing_box_fldID = "fld-fe868108cdd844d895ea982e820b346c"; // text
var boxes__boxes_fldID = "fld-74466a4b96dc43b4bcbe153646b76911"; // form
var boxes__tracking_number_fldID = "fld-54af5c1e20e14d20a7c82a40679a2a8a"; // text
var boxes__shipments_in_box_fldID = "fld-bc84b43e3cf649cdb27e4085b401aa2b"; // form
It makes it clear which field a variable is from because I ended up running into situations where it was hard to keep track of the forms for which a field applied. It also includes the field type as an extra hint which is sometimes useful to be reminded about.
The database I use for some of my work has
1. a form which contains client contact details (this has separate sections for contact details for company clients and then individual clients)
2. a second form which contains the individual jobs
3. there is a Link to Form field from 1 to 2.
In the jobs form I have a section which contains details of invoicing and I want to pull in the first name and address from the client contact form.
There are separate first name and email addresses fields in the client contact form for companies and individuals.
In the jobs form I wish to create a script the text of the email and would use the first name and address within the script (which I then export as CSV, and using Keyboard Maestro automatically create the email and automatically attach the invoice)
I have created a script where I have pulled in the fields from the client contact details and wish to create an if statement which selects the first name and email of whether the client is a company or an individual.
However, the script only produces ‘undefined’ although I know when I use the actual fields from the client contact form the console.log returns the correct information.
The script is as follows:
// Form ID
var client_contact_details_id = 'fld-c53e48e48e3a40c7a5656ab92f39ecc9';
var result = '';
var EmailAddressRecord = record.getFieldValue(client_contact_details_id);
var email_id = 'fld-8f9a928a7d554527b4127ef811136210';
var emailaddressfield = EmailAddressRecord.getFieldValue(email_id);
result = emailaddressfield;
result;
var client_first_name_and_email_address_details = record.getFieldValue(client_contact_details_id)
//Company details
// company name
var company_name_id = 'fld-a3300e3db044421598ae171c0a2d4834';
var company_name=client_first_name_and_email_address_details.getFieldValue(company_name_id);
// first name of company contact
var first_name_company_contact_id = 'fld-24ee4dae33d941bf81e1667853a7a949';
var first_name_company_contact=client_first_name_and_email_address_details.getFieldValue(first_name_company_contact_id);
// email address of company contact
var email_company_contact_id = 'fld-8cfa7613f04f4a8b82652bc27f3c05df';
var email_company = client_first_name_and_email_address_details.getFieldValue(email_company_contact_id);
//Individual details
// first name of individual
var first_name_id = 'fld-45599a4f0a6342d8973faed53dfbfed8';
var first_name = client_first_name_and_email_address_details.getFieldValue(first_name_id);
// Email address of individual
var email_id = 'fld-8f9a928a7d554527b4127ef811136210';
var email_individual = client_first_name_and_email_address_details.getFieldValue(email_id);
if (company_name == null) {
var Dear_Name = first_name_company_contact;
var Email_to_send_invoice = email_company;
} else {
var Dear_Name = first_name;
var Email_to_send_invoice = email_individual;
}
// console.log(yesnot);
console.log(Dear_Name + " " + Email_to_send_invoice);
Running the script produces:
24/03/2021, 11:36:03 / Notarial Act / Email adress for invoice
undefined undefined
Help would be gratefully received.
With a bit of practice I think you can get there. Scripting in Tap Forms unlocks a large amount of power to add custom workflows and interactions, so if you’re able to get the knack of it you can do a lot more.
I did a follow up video on the pick list idea as well as including some other suggestions from Brendan as well. The pick list approach is a little more nuanced and I must admit it’s a great suggestion because I use pick list based record colouring in a few of my own forms.
I did a quick video and posted on YouTube walking through create a new form with a couple of fields, creating a new script field that can be used to set the record colour and then I walk through using a calculation field to make the script a little simpler.
Here’s the final script from the video to make it easier to copy that portion in (don’t forget to change the record.getFieldValue lines to your own field IDs):
function Record_Colour_Setter() {
var rating_date = record.getFieldValue('fld-072414fe5ab04e93aba36c1051dc1f22');
if (!rating_date) {
record.setRecordColor(null);
form.saveAllChanges();
return;
}
var date_difference = record.getFieldValue('fld-14c4599832f74205a842c2c4d666709b');
if (date_difference <= 0) {
record.setRecordColor("#FF0000");
form.saveAllChanges();
} else if (date_difference >= 90) {
record.setRecordColor("#00FF00");
form.saveAllChanges();
} else {
record.setRecordColor("#FFBF00");
form.saveAllChanges();
}
}
Record_Colour_Setter();
Thank you Sam Moffatt,
What yon suggest seems to be a good solution. So, the million dollar question, how do I actually setRecordColor for a script cell and how do I make it compare cell content to the current date? I am very new to Tap Forms and not really much of a techy – so need simple “do this, then do that” type instructions please…
Cheers
It works for me. What you don’t see anymore is the ‘alias arrow’ on the thumbnail. Brendan had to change some stuff in order to add Javascript API support for this feature.
But look at the size of the file. If it is an alias, it is about 700 bytes.
Attachments:
You must be
logged in to view attached files.
Cheers, Daniel
---
See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricks
Thank you again, Daniel (and Brendan, before).
I made some trials:
-> the first (yesterday’s) version worked fine. :-)
-> the second (today’s) version produced a rather buggy result:
While the form ‘Email Received’ contained the records:
BBB@abc.ch
CCC@abc.ch
EEE@abc.ch
(see attachment)
the script pretended to have found an issue with the record ‘CCC@abc.ch’ (which I don’t understand), and pretended to have found a match with ‘EEE@abc.ch’ (which is plainly wrong); see the other attachment.
Any idea, why?
Attachments:
You must be
logged in to view attached files.