Is it possible to nest calculation formulas? For instance:
IF ((A=B OR C=D AND E=F);X;Y)
I don’t see any OR or AND type functions in calculations. Is there a different way of accomplishing this?
I’m utterly clueless in Javascript so I’m hoping that isn’t the only answer. If it is, I’m afraid I won’t be able to set up half the calculations I need.
Nothing wrong with the script, just saying you can format it a little better on the forum :)
I think you can do it in either a calc or a script but I feel a script would be easier to manage and debug.
Create a new script field, open up the editor and you should get something like this:
function Final_Amount() {
// Replace with your own code
var hello_world = "Hello World!";
return hello_world;
}
Final_Amount();
Wipe out the middle bits so it looks a little closer to this:
function Final_Amount() {
}
Final_Amount();
Put your cursor in the middle and use the editor to import all of the fields you’re interested in (3,5 and 7; 8 will be your script field). It should look something like this:
function Final_Amount() {
var amount_due = record.getFieldValue('fld-1234');
var amount_paid_minus_izettle = record.getFieldValue('fld-1337');
var amount_paid_minus_bank_charge = record.getFieldValue('fld-4321');
}
Final_Amount();
The exact names and ID’s will be specific to you. Then you just need to convert the logic you wrote into Javascript which is easy enough:
if (amount_paid_minus_izettle == 0 && amount_paid_minus_bank_charge == 0)
{
return amount_due;
}
if (amount_paid_minus_izettle > 0)
{
return amount_due - amount_paid_minus_izettle;
}
if (amount_paid_minus_bank_charge > 0)
{
return amount_due - amount_paid_minus_bank_charge;
}
return 0;
This needs to be embedded into the function, so your final script looks a little like this:
function Final_Amount() {
var amount_due = record.getFieldValue('fld-1234');
var amount_paid_minus_izettle = record.getFieldValue('fld-1337');
var amount_paid_minus_bank_charge = record.getFieldValue('fld-4321');
if (amount_paid_minus_izettle == 0 && amount_paid_minus_bank_charge == 0)
{
return amount_due;
}
if (amount_paid_minus_izettle > 0)
{
return amount_due - amount_paid_minus_izettle;
}
if (amount_paid_minus_bank_charge > 0)
{
return amount_due - amount_paid_minus_bank_charge;
}
return 0;
}
Final_Amount();
That should do mostly what you want, I’d go for a script because it’s a little easier to debug and build up plus easier to format out than the calculation fields.
hi Sam thanks for replying
is there something I’ve done wrong in my script?
Sorry I’m very new to coding in javascript and I’m not sure what you mean
“You can use backticks to wrap your code
thanks
Rainer
Sam,
Thank you for the response.
…are you expecting to replace a substring of numbers to a new value in your number field?
Yes that is right.
I can see there is a string function in JavaScript but because I do not know JavaScript, could you amend the code so that it is possible to search for one or more numbers? I would be very grateful.
Hello Vera,
What was it about the Invoices sample database document that didn’t help? The description of your requirements sounds awfully familiar to the Invoices sample. There’s a Clients form (yes, like a table), a Products form, and an Orders form, which is essentially your invoices.
You can filter records based on any fields that are in the parent form. I call these filters Saved Searches. You can even have different sort orders and field columns displayed for each Saved Search.
You can see on the sample screenshot attached that also shows the sub and grand totals.
There’s an advanced find and replace function in the Mac version which would let you do a global update. Plus there’s other tools for that sort of thing too such as the fill up/down and drag fill function, like you get in Excel or Numbers for copying the value of one cell down to others in the same column.
Hope that helps a bit.
Thanks!
Brendan
Attachments:
You must be
logged in to view attached files.
The problem is that replace is a method of a string which in Javascript is an object. A number is a different type of object and doesn’t have a replace method because it doesn’t make sense.
To help you along, are you expecting to replace a substring of numbers to a new value in your number field? To do so, you’d have to convert the number to a string (easy way is to use an empty string and add your variable to it, e.g. '' + myvar) and then replace would work fine because it’ll coerce the number into a string form.
Hello all
found a way to get this done with this script
I thought I’d post it here incase it helps someone else
function Try2() {
var call_date_id = ‘fld-9b94287517344436995393d16d0f5d7e’;
var mydate = record.getFieldValue(call_date_id);
var startdate = new Date(mydate);
var enddate = new Date(startdate);
enddate.setHours((startdate.getHours() + 1));
console.log(enddate);
}
Try2();
Hello, I am trying to add one hour to the value from a date time field
and put it into a variable recdate
here is what I’ve tried but I keep getting an error
any help would be appreciated
Try2() {
var call_date_id = ‘fld-9b94287517344436995393d16d0f5d7e’;
var mydate = record.getFieldValue(call_date_id);
var recdate = dateadd(mydate,0,0,0,0,1,0,0);
return recdate;
console.log(recdate);
}
Try2();
returns error
-05-022020, 1:01:26 PM / Daily copy / Try2
Try2: ReferenceError: Can’t find variable: dateadd, line:(null)
The above post needs clarification: the code only works on a text type field and not in a number type field. I would like to know how the script should be amended to work on a number type field
I am getting the following error:
02/05/2020, 18:24:01 / Time spent / add 0s to Time charged for (not empty)
add 0s to Time charged for (not empty): TypeError: value.replace is not a function. (In ‘value.replace(find_text, replace_with)’, ‘value.replace’ is undefined), line:(null)
when running the following script
var time_charged_for_id = 'fld-755fd264b59b42e59c7254edf03ea281';
function findAndReplace(find_text, replace_with) {
for (let rec of form.getRecords()){
let value = rec.getFieldValue(time_charged_for_id);
if (value) {
rec.setFieldValue(time_charged_for_id, value.replace(find_text, replace_with));
}
}
form.saveAllChanges();
return;
}
findAndReplace(/[a-zA-Z0-9]*/, '0');
I created a test database and it ran and did what is should. But in a working database it is causing the error. The field type is the same (a number field) in the test database and the working database.
The code was provided by Daniel Leu in the exchanges at https://www.tapforms.com/forums/topic/find-and-replace-script-to-replace-anything/.
Because I do not really have any understanding of JavaScript I cannot tell why it is not working.
I would grateful for any help in identifying what is wrong.
Most of the Javascript references that are basic Javascript should be relevant to Tap Forms. There are some functions that are not part of the Javascript ES6 standard that web browsers commonly implement that you might not see. Most of the time anything that references window, document and the DOM will not be relevant with Tap Forms. Unfortunately Javascript’s use in web browsers isn’t entirely standardised which leads to confusing documentation on the web.
In a form I have the following fields:
1. Invoice amount [the amount I invoice for a piece of work]
2. Amount paid [the amount a client pays]
3. Amount due [which is a calculation field – 1. – 2.]
4. iZettle charge [if the client pays by iZettle then iZettle charge]
5. Amount paid into bank account after iZettle charge [a calculation field – 3. – 4.]
6. Bank charge [if my bank makes a charge the bank charge]
7. Amount paid into bank account after bank charge [a calculation field – 3. – 6.]
8. Final amount received [which will be either 3., 5. or 7. 8. will then be use in various reports.]
I would like to know whether it is possible to use a calculation or rather if I should use a script
to achieve.
To put into 8.
1. if 5. and 7. are both zero than 3.
2. if 5. is not equal to zero then 3 – 5
3. if 7. is not equal to zero then 3 – 7
For either I would be grateful with how I can construct the appropriate calculation or script.
Daniel,
Apologies for my comments, they were not intended to disparage your helpful replies – more my frustration with trying to understand the pages you linked (mainly to do with JavasScript for websites) and its relevance to Tap Forms (and the sparse practical information currently available) for someone who does not understand it.
Anyway I have finally worked out where to put
findAndReplace(/[a-zA-Z0-9]/, ‘replace’)
That is right at the end of the code you provided and now provides what I need. Thank you for providing code.
To potentially work around this, you could use a script field instead and have the script field update the value of the other fields correctly. That way you only have one thing executing (your script field) and it’ll update the fields correctly. You’ll then have to convert the calc fields into normal number fields to finish everything up.