When 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&tricks
Seeing 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 undefined
This is on same search, same record, just running the script from menu vs button.
Is there any way to copy a photo field image to the clipboard via script?
I can’t figure out how to retrieve the image from the blob object and then copy it to the clipboard.
Basically need an image equivalent to Utils.copyTextToClipboard
I can see the digest but that’s about it…
var cover_id = 'fld-xxx';
var cover = record.getFieldValue( cover_id );
return cover[0];
I 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);
Thanks for the script Brendan. Glad to know about the new feature for a future release. If I can get this working, my goal is to share it with other land managers and ranchers in Texas for them to use. We all have to submit a report to the County at the end of each year and it can be a pain going through receipts and trying to remember everything. This could help keep things organized.
Here’s the form template. The only problem with it is if you switch records, the Province pick list would not be synced with the Country selection until you actually changed the country.
FYI, I have already built a new cascade pick list feature into the next big version of Tap Forms I’m currently working on. No ETA though. And no scripting required for it.
Attachments:
You must be
logged in to view attached files.
Here’s a script that I was messing around with that will change the Province field’s Pick List depending on the value selected from the Country field’s Pick List:
function Province_Pick_List_Switcher() {
var country_id = 'fld-de7985c881804154b037c68dc4c24414';
var country = record.getFieldValue(country_id);
var canadian_provinces_pick_list = document.getPickListNamed('Canadian Provinces');
var us_states_pick_list = document.getPickListNamed('US States');
var province_id = 'fld-b1458c16690744129ff145f7b9607c99';
var province = form.getFieldWithId(province_id);
if (country == 'Canada') {
province.pickList = canadian_provinces_pick_list;
} else if (country == 'United States') {
province.pickList = us_states_pick_list;
} else {
province.pickList = null;
}
}
Province_Pick_List_Switcher();
You could modify this script for your own needs. I added it as a Script Field and then hid the Script Field so it doesn’t show on the form, but it still works.
Ok, I’ve created a new database called Wildlife Exemption and a form called Wildlife. I’ve created all my fields, one of which is a Pick List field called “Management Practice”. I created a corresponding pick list also named “Management Practice”. There are 7 values in the “Management Practice” pick list. One of the values is “Habitat Control”.
I have created pick lists for each of those 7 values that I am referring to as “sub practices”. So, for example, I have a pick list called “Habitat Sub Practice”. In that pick list are things like Grazing Management and Brush Management.
The idea is that when the user is on the “Management Practice” field, they are presented with the list from the “Management Practice” pick list. They have 7 options, but let’s say the choose “Habitat Control”. The next field on the form is called “Sub Practice”. In this field, the pick list “Habitat Sub Practice” would be chosen because of the selection above.
The only way I can think of to do this is with a long nested IF THEN kind of routine:
IF Management Practice = “Habitat Control” THEN “Habitat Sub Practice”
ElSE IF Management Practice “Erosion Control” THEN “Erosion Sub Practice”
and do 5 more ELSE IF’s so that all 7 Management Practice options are covered.
I’m guessing that this is probably not the smartest way to go about it, and I’m sure I have the syntax way off. I’ve gone through T.L. Ford’s Java Scripting 101 and that has been very helpful, but my only programming experience is with BASIC decades ago in High School.
Would someone be able to get me kicked off with the start of a script here?
Thanks,
Brent
Brendan Hi Ok Ill think about this. I guess there is no problem to insert a Record in a different form than the form with the field script, correct ? Ill give it a try and post here the results.
Bernie
Field scripts execute whenever a value they’re monitoring changes. So if you’re entering data anyway, you could monitor for that change and insert the records you want at that point. You don’t even have to be doing anything with the field you’re monitoring. Just to know that it has changed and the script will be triggered. But no script will get triggered just by viewing a record.
Brendan Hi thanks for the opportunity to explain. I will try to be brief but somewhat detailed.
I have 3 linked forms
1. Drill Description
2. Drill Results (One record for each time a drill is executed)
3. Drill Total Results (One Record for each Drill with accumilated totals of rows in number 2 above. Totals are calculated using Calculation of linked fields
One time process : Load a description for all possible drills in Form 1 assign a ‘DrillID”
Eacb time a drill is executed :
1. Open form 1, drill description
2. Pick a drill and click on the + next to linked Form Drill Results (2)
3. Enter Results and either pick a new Drill Description or exit
The Problem as I see it. The records in table 3 (Drill Total Results) need to be manually added the first time a drill has results which for me, is extra work on the user part I like to avoid. As if the user forgets to pen the Form and add a row, no totals will ever be displayed.
What I have done is write a script that will look at all the records in table 2 (Results) and check if a Record exists (by Drill ID) in Table 3 (Totals), if exists, do nothing, if not exists add new row. Now when a Drill Description is open the linked forms for both Results and Totals will be present and viewable.
This script has to be executed manually using the Form Run Script command. It would be nice if it could be executed ‘ON Open’ automatically.
Or is there another way to accomplish script execution without user intervention?
Hope this is clear.
Thanks for the help
Bernie
These MOASS Ladder templates have gotten some revisions..
(1) Mainly I had forgotten to connect the fractional shares field, and when corrected that blew three scripts, all revised.
(2) Then also I have added a way to re-use the Goal Section within multiple Ladders. Usually your goal list would remain static.
New versions below.
Are there any available hooks in a Form such as ‘ON Open” o ‘On Close’ etc. Where one could have a script execute based on this event?
Thanks
Hi David,
You can do this with a Calculation field, but it would be much easier to understand with a Script field.
Just off the top of my head without actually testing it out, here’s a script that “should” work:
function getResult() {
let margin_id = 'fld-....';
let money_id = 'fld-....';
let margin = record.getFieldValue(margin_id);
let money = record.getFieldValue(money_id);
var result = 0;
if (margin < 25)
result = 0;
} else if (margin >= 25 || margin <= 27) {
result = money * 0.08;
} else if (margin > 27 || margin <= 30) {
result = money * 0.1;
} else {
// not sure what happens if margin is greater than 30?
}
return result;
}
getResult();
So add a Field Script and copy this code into your script and then change the field IDs appropriately to match the actual field IDs of your own Margin and Money fields.
It should work.
Thanks,
Brendan
Ok, I am trying to learn some scripting for the calc field but I am running out of time in my evaluation process of TF (which so far has been amazing) and we have a use case that we can do in Numbers but unsure if we can in TF.
I am trying to do an if/then usineg two fields. One is a Margin field (which is a number field) and the other is a money field. Basically I am trying to get a final calculation from certain numbers. It looks like this
I the (Margin) is <25 then 0
If the (Margin) is > or equal to 25-27 then (Money) * .08
If the (Margin) is > or equal to 27-30 then (Money) * .10
Is this possible in the calculation field and if so…If someone would be willing to show me how it will help a ton with me starting to understand exactly how this scripting works.