Interesting question…. I would create a separate form that has fields for ProductID, Batch# and BatchUpdated (date). This is used to track the current batch number and when this batch number was last updated.
In a form script that updates the batch number, I would check if the batch number was already been updated this month. If this is the case, then it is just a matter of incrementing the batch number. Otherwise, reset it to 1. At the end of the script, update the BatchUpdated field with the current date.
When creating a new record for your production run, you can fetch the current batch number from the batch number form.
Hope this helps!
Cheers, Daniel
---
See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricks
I’m wondering if you have to select a form from AppleScript first? But I don’t know how the syntax would look like.
To get a valid record, the question is, which record are you looking for? The first one, a matching one, a new one?
Here is the concept to get to the records without having a valid form. I assume that this is the case for your since you only open a document with AppleScript.
var records = document.getFormNamed('form name').getRecords(); // get all records of given form.
// loop over all records
for (record of records){
console.log(record.getFieldValue(time_id));
}
Cheers, Daniel
---
See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricks
Thanks, Sam.
Can you give me a syntax in AppleScript or JavaScript for selecting a record?
Hello All!
I’m trying to write script for a field that will auto populate a batch # for products being made. The format for the batch # will be Product ID #-MMYYYY-Batch #.
Product ID # – this is a number I have already assigned to every unique product that can be made
MMYYYY – this will be the month & year for when the product is made
Batch # – I want this to be an auto-incrementing # that starts at 1 (or 01 or 001) for each unique Product ID # a record is created for.
So, for example, the first batch of a product (lets say product 12) made in a month would have an overall batch # of 12-112019-1 (or 01 or 001). The first batch made for a different product (lets say product 25) would have an overall batch # of 25-112019-1 (or 01 or 001). The 2nd batch made for any given product would have the same beginning numbers as the 1st batch of that product but that last batch # component would increment to 2 (or 02 or 002) and so on. When a new month comes around that batch # component would reset back to 1 (01 or 001) for products made in the new month. So for the first batch of product 12 in the new month the overall number would be 12-122019-1 (or 01 or 001).
I have been thinking about this for the past few days but have not come up with any ideas on how to accomplish it.
If anyone has any advice or ideas I would appreciate it
Thanks!
Form scripts can be executed without a record being selected. What you’re running into is that there is no record selected and as such record is undefined.
It’s more likely to be seen on iOS where you can run form scripts from the list view without anything selected but as you’ve found out it is also achievable on the desktop as well, just less likely.
Following an email exchange with Brendan, and some further unsuccessful experimenting, I tried to use AppleScript to open a script in a new, absolutely minimal database created for my other current query in this forum about scripting time data:
Form Script “New Script” is:
function New_Script() {
var time_id = 'fld-33fcface42864bf3801d7bf521c14e7f';
var thisTime = record.getFieldValue('fld-33fcface42864bf3801d7bf521c14e7f');
console.log(thisTime)
}
New_Script();
When run, it puts a time value into the console (albeit, as discussed in the other thread, an hour earlier than the value in the originating field).
However, when I use this AppleScript to call it:
tell application "Tap Forms Mac 5"
open "/Users/martin/Library/Containers/com.tapzapp.tapforms-mac/Data/Documents/Time testing.tapforms"
set doc to document "Time testing.tapforms"
run doc script named "New Script" in form "New Form"
end tell
I get the following console message: “New Script: TypeError: undefined is not an object (evaluating ‘record.getFieldValue’), column:23, line:4”. Column 23 of line 4 is the dot in record.getFieldValue.
I suppose this is an improvement on nothing happening (!), but I don’t understand why this should happen.
I’m in the UK, and my Mac’s Date/Time is set to auto, which means GMT currently.
Getting the time calculation right in this database is crucial, because the info will be exported to tell musicians what time to turn up for a gig! I look forward to seeing how you folks resolve these problems (opportunities!).
So, related to this question – in fact the reason why I went down this path in the first place – is the issue of how you combine info from a Time field and a Date field to produce a Date
object that can be entered into a Date & Time Field. I’ve tried all sorts of JavaScript instructions, but TapForms doesn’t recognise any of the results as a valid Date-Time that can be used to set a field. Is there a simple method? – something I’m missing, please.
It shows 11:06:26 AM for me and the script reports Thu Jan 01 1970 11:06:26 GMT-0800 (PST).
At a first glance this is okay, but the console reports PST instead of PDT. PDT is UTC -7. Now I’m confused too…
Cheers, Daniel
---
See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricks
Thanks, Brendan.
I’ve just created a new document – one form, one (Time) field, one script
I used the field time tool to enter the current time (20:06:26).
Running the script gave me a console output of “Thu Jan 01 1970 19:06:26 GMT+0000 (GMT)
“, which is one hour before the field value.
Document attached. I’d really like to know what’s going on!
Attachments:
You must be
logged in to view attached files.
Time fields always set the date portion to 0 time, which is January 1, 1970. But your time zone can definitely change how that appears.
When I added a script to get the time, this is what I get when I log to the console:
Thu Jan 01 1970 12:41:38 GMT-0700 (MST)
I had entered 12:41:38 PM into the field.
The output of your script:
function Time_Script() {
var time = record.getFieldValue('fld-58b1679f309d4e848f3aaf8f1d22da45');
if (time) {
console.log(time);
var thisHour = time.getHours();
var thisMinute = time.getMinutes();
var newTime = thisHour + ":" + thisMinute;
console.log(newTime);
}
}
Time_Script();
for me was:
Thu Jan 01 1970 12:41:38 GMT-0700 (MST)
12:41
So the same time values.
Daniel,
I should, of course, have checked this first. The answer is that console.log(thisTime) reports “Wed Dec 31 1969 23:14:00 GMT+0000 (GMT)”. So the issue is with the first step (extracting the data from the Time field), not with the getHours() function.
To be sure, I’ve just created a completely new Form, with one record, one field, and one script:
var thisTime = record.getFieldValue('fld-7c2e0886dfa54aa59ffb75012ddd49b1');
console.log(thisTime)
I used the time tool attached to the field to enter the current time (17:22:50). Running the script output Thu Jan 01 1970 16:22:50 GMT+0000 (GMT) to the console.
If you are pulling from a JOIN field, you need to get the records in that JOIN. JOIN is a many to many type so you will get multiple records back:
var joinRecords = record.getFieldValue('fld-nameofjoinfield');
This will return you a list of records that you need to iterate over. If you look in the snippet editor there is a “child records loop” snippet that should help you if you select the field you care about and then do something useful with it.
Depending on your use case, the result of last line of your script field will be used as the script field value. If you’re looking to pull values out then you can just leave a line with the variable in it that you want to show up in the field.
Here’s an example of how you can combine multiple values from child Link to Form fields into a single value, this feels like it might be pertinent to your use case.
@Daniel: I would not expect it as well, but it is the case
Just tested in a testform again:
function Test2() {
res=record.getFieldValue('fld-76b2c64fccc34c20b62b7d485f5bfa28');
return res;
}
Test2();
This script will detect res as undefined in a listview, what is normal.
Opening one recordset and going back to the listview the script will show the field value of the last opened record.
Means, record is not blank once opened a record and switch back to the listview.
Possible this is only on iOS the case, but for me it’s a bug.
@Brendan ?
Yah, if you’re in the list view then record is empty because you don’t have a record selected. On the desktop this is less of an issue because it’s very hard to never have a record selected but on iOS if you’re inside a pure list view record is blank. I’ve been caught by that before on iOS by running scripts from a list and getting a pop up reminding me I need to be inside a record.
I need to manipulate Time data in scripts. One problem is that when I use the JavaScript getHours() function, it subtracts an hour from the value in the field.
var thisTime = record.getFieldValue('fld-95075cb1f5604be59afbc73094591e7a');
var thisHour = thisTime.getHours();
var thisMinute = thisTime.getMinutes();
var newTime = thisHour + ":" + thisMinute;
console.log(newTime);
The field is showing 00:14. The console result is 23:14.
I’m in a GMT zone, and the Mac’s Date & Time settings are on auto.
Does anyone have an explanation / solution, please?