Hi Glen,
Thank you for sharing your form! The script Brendan posted is for a field of the type Script and not Calculation.
Here is the updated script that works for me:
function Season_to_date() {
var records = form.getRecords();
var currentRecordIndex = records.indexOf(record);
if (currentRecordIndex > 0) {
var previousRecord = records[currentRecordIndex-1];
} else {
return 0;
}
var today = record.getFieldValue('fld-61c03b767f2c497491d10a28db8abdb3');
var previous_total = previousRecord.getFieldValue('fld-f6bc7e82da874579940fb8afc167dac4');
var total = today + previous_total;
console.log("Today: " + today)
console.log("Previous: " + previous_total)
console.log("Total: " + total)
return total;
}
Season_to_date();
Cheers, Daniel
---
See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricks
Hi Glen,
You put that script code into a Calculation field.
You need to put it into a Script field, not a Calculation field.
Thanks for the script, but it doesn’t do anything, I suppose be cause I have to define where to get the previous record and then add today’s total to it. I could probably spend the rest of the day working on this and not figure it out I’m sure.
I’ve attached my file and maybe if you fix it I can apply the example to other similar forms I have to import data for.
Thanks.
Attachments:
You must be
logged in to view attached files.
I’m trying to extract the phone number from a linked form.
The current form is called Appointments and the field is simply called “Phone”
The linked form is called “INSTALLS” and the field I want to draw from is called “Best Phone”
This is the error I get when I run the script:
2023-01-13, 8:27:29 PM / Appointments / Phone
Phone: SyntaxError: Invalid character ‘\u2018’, line:3
and this is the Script:
function Phone_Script() {
var Phone_id = ‘’fld-1a146d1f63aa43c0881af16b1e65f1be;
var Best_Phone = record.getFieldValue('fld-eef8580ecdb744bf94a58eb1b7997a89');
if (Best_Phone != undefined) {
var Phone = Best_Phone.getFieldValue(Phone_id);
return Phone;
} else {
return "";
}
}
Phone_Script();
Any suggestions?
-
This topic was modified 3 years ago by
Brendan.
There’s no function in the Calculation field to reference the previous record. Calculations work only on the current record.
You would probably have to use JavaScript to do that. But then, what really is the previous record? It really depends on how you sort your records.
Hi Glen,
Instead of JavaScript, you could also just use a Calculation field.
In your previous JavaScript question, the solution to get the difference between two dates in months is this formula:
MONTHS(Start Date; End Date)
and that’s it!
It’s not required to learn to program in JavaScript just to do simple things like that. That’s what the Calculation field is for. There’s lots of functions that’ll do all sorts of things for you without programming. You just have to know simple mathematical expressions.
Thanks,
Brendan
Hi Glen,
You would not use the square brackets around [today] and [purchased].
In JavaScript there’s a getMonth() function you can use on dates.
So your formula would be:
var duration = today.getMonth() - purchased.getMonth();
FYI, NaN stands for Not a Number
Disappointed…
I didn’t know I would have to spend a week or more to learn Javascript to add or subtract two fields. Spent the greater part of a summer recently learning “R”, but now I can barely get started again. Use it or lose it. I don’t want to spend a week to do a simple calc to get a form to do something and then a month or year late have to go through all that again. This isn’t simple…
I’m learning!
Okay so I took the old code and tweaked it to fit another line of data. The format does not need to change because it is simply a Student ID Number. I got it to spit out the same number but as a script. The scripted output is now my form and cannot be edited from the custom form.
This is what I did;
function formatStuID(val){
val = val.replace(/(\d)-(\d{4}).*/, '$1-$2');
return val;
}
function Formatted_Sin() {
var sin =
record.getFieldValue('fld-66039384e7ac46f0aecb546f5e8c4ac9');
console.log(sin);
if (sin != undefined) {
return formatStuID(String(sin));//"1234567890"
} else {
return nil;
}
}
Formatted_Sin();
BAM!!! Protected form value!
Hi Matthew,
There’s no function to lock a field on a custom form. You can lock the position of a field so you don’t accidentally move it. But that’s not used for preventing editing on a custom layout. But what you can do is just not include that field on your custom layout and it will still be available on the Default Layout. With custom layouts you don’t have to have every field available displayed there. Not even for script or calculation fields to work. They can be on your custom layout even without any of the fields they reference. And you can create as many custom layouts as you like.
Thanks,
Brendan
So I am new (as you may know).
I went onto a coding adventure to output a value in a desired format.
When I add a new record there are some values that are required for each record.
Optimally I do not want those to ever be accidentally changed. So far being new to TapForms I have had to be very carful when using a copy/paste from MY form so I don’t change a value. When I enter a new record I use the default layout but when I dig in and use my data I use MY custom form.
I do not know if there is a way to lock a value on a custom form but what I learned is that the output value of a script on MY custom form cannot be altered easily.
So my silly question is, can we lock a value so it cannot be changed on a custom form but only from the default layout?
Oh, and the Script must be set to return a Text result.
You could actually parse the SIN numbers from one field, then format them using the script to display in another field.
Here’s code to do that. Make a new field and set the Field Type to Script. Paste in this code:
function formatSocialSecurity(val){
val = val.replace(/\D/g, '');
val = val.replace(/^(\d{3})/, '$1-');
val = val.replace(/-(\d{2})/, '-$1-');
val = val.replace(/(\d)-(\d{4}).*/, '$1-$2');
return val;
}
function Formatted_Sin() {
var sin = record.getFieldValue('fld-4289784906dd4822ab1b738c404ca783');
console.log(sin);
if (sin != undefined) {
return formatSocialSecurity(String(sin));//"135-71-1458"
} else {
return nil;
}
}
Formatted_Sin();
I didn’t make up the formatSocialSecurity() function. I Googled for it.
You would have to replace your Social Security Number field ID with the one from your own form.
The script requires the values to be Text, so this part converts it to a String: String(sin)
See if that works for you. You may need to modify the code a bit to get the right format for you.
Hi James,
There’s no function in Tap Forms to write to the file system from a script. But adding write ability is something I’d like to do at some point.
Although you can post to a web service, so maybe if you had some web service running on your Mac you could have that service write to the file system after receiving data from Tap Forms.
Thanks,
Brendan
Hi! Is it possible to have tap forms create a new folder in finder named using data from tap forms fields?
I would love to click a button in Tap Forms that would then create a new folder with a LastName_FirstName, then another folder inside with OrderNumber, Equipment Folder 1, Equipment Folder 2.
Thanks for helping me find out if this is possible via Apple Script or another method :)
James