Thanks Brendan,
This helps me understand the picklist programming. I’ll play around with it.
Unfortunately it needs a document close/open. I really need an ability to have a dynamic picklist on a record by record basis.
BTW I have really enjoyed the increase in power with scripting in Tap Forms. My favorite app.
Hi Brendan,
yes, that is what I need.
Do you have an idea, how that script looks like?
I am trying to understand how to use the scripting commands to automate some searches, but not able to understand from the available documentation how to use them.
Example:
1. I have a form called Time spent, one of the fields is called ‘For duplication’. For some records the only entry is a “X”
2. I have created a saved search called “For duplication” which only function is search records in the ‘For duplication’ field which contains an “X”
Queries
1. Is it possible to create a form script which uses the saved search called “For duplication”
2. Also how would I create a search with a script to search a field for a value (such as search the “For duplication” field for a “X”.
Any help gratefully received.
Hi Gerhard,
Do you mean you have a 3 level hierarchy and you want to show data from the lowest level to the top level?
Like Parent -> Child -> Grand Child and you want to show data from Grand Child on the Parent form?
You can do that with a script, but the way the data would be displayed would be just in a text format, not in something like a table like regular Link to Forms are displayed.
Hi Johnny,
Sorry for my delayed response.
Improving the JavaScript API to support more Pick List functions such as pickList.addValue('Sci Fi') or pickList.addValues(some_values_array); is on my toDo list.
But I haven’t worked on that yet. In the meantime, something like this will work, but it does seem like you still have to close the document and re-open it for the new values to appear:
function AddToPickList() {
var pick_list = document.getPickListNamed('Condition');
var pick_list_values = pick_list.values;
// print the current set of values. You can see the dictionary format you need. You can optionally provide a <code>valueColour</code> (yes, Canadian spelling) property key too. Use the colour format you see.
console.log(JSON.stringify(pick_list_values));
var new_value = {"value" : "My New Pick List Value"};
// add the new value to the values array.
pick_list_values.push(new_value);
// reset the list of values with the array that contains the new value
pick_list.values = pick_list_values;
// saving on the pick_list
pick_list.saveChangesInDatabase();
// log our new set of values.
console.log(JSON.stringify(pick_list_values));
}
AddToPickList();
In a future update I’ll add a better API for Pick Lists. I’ve tested this and it does work.
Hello together,
is there a way, to combine more than two linked forms with “m to n” or “1 to n” relation?
The following task: Main form: Company
Goal: Project tracking with an “m to n” link. I would like to create a project tracking, there I need the users-name, for every project the cal-dates, notes, different pick lists and other things. Then I need to show the results in the higher-level Company form. However, I cannot do this with the previous tools from TabForms. Can something like this be realized with a script?
Best regards,
Gerhard
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.