Hi All. Loving TapForms so far,
Is there any way to create a script that will run automatically when a record date field from a table = todays date?
Any help gratefully revived
Cheers
Jon
-
This topic was modified 3 years, 2 months ago by
Jon Millar.
for future readers as much as OP:
Can someone recommend any books, videos, etc. to learn JavaScript, particularly in relation to Tap Forms 5 rather than web use?
I used Khan Academy to teach myself further maths; they have a course on JavaScript. I found it better than a real tutor (pause + rewind). Probably start with a few modules here.
I learnt two ways:
– downloading the source of a website, modifying it, breaking it, fixing it
– setting a goal of writing a script to do something useful on a webpage automatically – in my case it was logging into a booking website every day, finding available time slots, and booking the first slot before anyone else
I can suggest tools but to start experimenting just enable the developer menu in Safari, and open inspector/console.
Tap Forms API reference:
tapforms.com/help-mac/5.3/en/topic/javascript-api
Tap Forms scripting videos (fairly in-depth, probably don’t start here)
pasamio YouTube
edit: format
I think the snippet is ok, it’s just if you run the snippet inside of a function it’ll insert it there. I think an improvement for a future prompter would be a way of having the callback accept the return values as a dictionary which would remove the need to have the variables defined at the right scope (which for Javascript is a challenge at times).
There are all sorts of functions you can perform on JavaScript Date objects without having to do the math yourself:
https://www.w3schools.com/js/js_date_methods.asp
Of course. How did I miss that.
It does work.
I have some work to do to understand the scripts you have given me, so thanks for that.
Also, thanks for the calc info “In Javascript, the Date object represents the time in milliseconds since January 1, 1970, UTC. So the difference needs to be divided by milliseconds-per-day.”
I will need that in another Form I just used the above script on and it worked. I have to know the weeks and months, not just days.
You need to put in your own field ID instead of fld-xxx as in Daniel’s sample script. He doesn’t know what your field ID is, so we often just put in stand-ins for a field ID. It’s something you have to substitute yourself when you see any code here. Field IDs are unique to your own forms.
Those of you who have been looking at my YouTube might have seen the video talking about Managed Fields and I’m finally ready to show it to the world. This is brand new, probably has a few bugs and definitely isn’t as battle tested as some of the other code I’ve written as I had a more immediate need to leverage it. It’s already up to 1.0.1 because I found a bug with the log feature (I forgot to include the record ID! Doh!) and also wanted to include my old setIfEmpty feature as a part of managed fields as well making it easy to only update fields whose values aren’t already set in addition to a couple of other options.
Those curious might also be interested in the VOD of Twitch stream I did which has some more development included though has some distractions as well. You can see in real time just how slow building some of this code up is though I must apologise the audio quality ended up quite poor.
A big part of Managed Fields are two accompanying forms in addition to the Managed Field script library. Check out the attachment to this post (you’ll need an account to download) and import it to your record alongside the original Script Manager. I’ve also started to add some Markdown documentation and you can see an example with the addToTableIfMissing. I’m slowly adding documentation for various things as I come back to using them and forget how they work so hopefully also sharing. At some point I hope to leverage the Markdown feature in Tap Forms to include these inside the database so the documentation is always available…even if you’re working on your Tap Forms document on the plane!
Attachments:
You must be
logged in to view attached files.
I got several errors when I tried to run your code. This touched-up version works for me:
var date_id = 'fld-xxx';
function Days_Between_Dates(){
console.log("Days_Between_Dates() called ...");
var days = -1;
var records = form.getRecords(); // calls function getRecords()
var currentRecordIndex = records.indexOf(record);
if (currentRecordIndex > 0) {
var previousRecord = records[currentRecordIndex-1];
} else {
return -1;
}
// get the two dates
const today = record.getFieldValue(date_id);
const previous_day = previousRecord.getFieldValue(date_id);
// calculate the difference between the two date objects
days = Math.round((today - previous_day) / (1000 * 60 * 60 * 24));
console.log("today: " + today)
console.log("previous day: " + previous_day)
console.log("difference: " + days)
return days;
}
Days_Between_Dates()
In Javascript, the Date object represents the time in milliseconds since January 1, 1970, UTC. So the difference needs to be divided by milliseconds-per-day.
I changed the return value to -1 for the first record.
When reporting a syntax error, it’s helpful to have the entire message.
When posting code, please encapsulate it in back-ticks. This way it is easier to copy&paste it and it looks prettier. Thanks!
Cheers & happy hacking!
-
This reply was modified 3 years, 2 months ago by
Daniel Leu.
Cheers, Daniel
---
See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricks
I see what you did. You copied Daniel’s code and put it inside your script after you had already defined the function.
What he meant by this part:
function Days_Between_Dates(){
console.log("Days_Between_Dates() called ...");
...
}
Was take your existing script and just below the function declaration, put in the console.log() call. Not duplicate the function definition again.
Then after your function definition, actually call the Days_Between_Dates() function which your original script did not show you were doing.
I’m a bit confused with your script. Did you define Days_Between_Dates() twice?
Or was that a typo in your post?
I also edited your post to put the back-ticks in so your code was easier to read.
-
This reply was modified 3 years, 2 months ago by
Brendan.
Do you have a Days_Between_Dates(); at the end of the script after the closing bracket of the code you showed? This causes your function to be called.
If you run this script on the first record, it will not do anything since it is the first record.
Sometimes I add something like this to my functions:
function Days_Between_Dates(){
console.log("Days_Between_Dates() called ...");
...
}
Days_Between_Dates();
This way I can easier trace what’s going on with my scripts.
Cheers, Daniel
---
See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricks
Ok, missed that.
But I got all the syntax cleared and this script does nothing, not even printing to console the variable values. So I’ve proved I haven’t learned anything yet.
Any ideas?
Thanks.
function Days_Between_Dates(){
var date_id = ('fld-119ad5683104451ca6855e777a23ad21');
var records = form.getRecords(); // calls function getRecords()
var currentRecordIndex = records.indexOf(record);
if (currentRecordIndex > 0) {
var previousday = records[currentRecordIndex-1];
} else {
return 0;
}
var today = record.getFieldValue(date_id);
var previous_day = previousRecord.getFieldValue(date_id);
var total = today - previousday;
console.log("today: ")
console.log("previousday: ")
console.log("total: ")
return total;
}
The record isn’t important enough to warrant work. But the next problem might be.
BUT, the fact that all those backups I have are useless unless I want to replace all my Forms and lose all the data in any or all 67 Forms since the last backup is appalling. Also, I guess I’ll never compact them again since I’ll never know when something might happen and I need that deleted information. I had compacted it because I had been doing some experimenting with Scripts and other features and after the session I was deleting records and Forms.
The only way to have a backup is to archive all 67 Forms individually? That is ridiculous.
Also, where is the option to engage a window “Are you sure you want to delete this record” with a yes or no/cancel option is almost unheard of in programs.
Disappointed… In the past with my other database program there were many times I had to resort to a backup file and replace the original with that file. That program backed up each “Form” into a separate file and was a real backup. Sure, it took longer to sync and backup, but it worked and your system doesn’t.
As I mentioned before, the return type of the script needs to be set to number.
-
This reply was modified 3 years, 2 months ago by
Daniel Leu.
Cheers, Daniel
---
See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricks
I thought I was done. I was double checking that Script field ID, and wondered why it didn’t show up in the script editor and then forgot about that.
I hit refresh and now I have the totals incrementing, but instead of adding the previous total to the current number, it is concatenating them.
One of the things I love about programming is the surprises you don’t expect because of flawed logic…
0 0
50 500
100 100500
100 100100500
100 100100100500
20 20100100100500
60 6020100100100500