Perform calculation is an action that runs a short piece of JavaScript and saves the result in a variable (the variable is called a custom variable, and later actions can use it like any record value). It's meant to help you change or combine values before you use them, without writing a full script.
You can use this action for many things, including:
Adding 14 days to a due date and saving the new date for a follow-up task.
Turning a first name and last name into one line for an email greeting.
Note:
It saves the value of the last statement. The code has to end with a value. Write
price * 1.19, notreturn price * 1.19. Areturnat the top level fails the run with the errorIllegal return statement.It runs synchronous code only. You can't use
awaithere. To wait for a result, useExecute script.The variable only exists after the action. Add
Perform calculationabove every action that uses its result.
Who can use the Perform calculation action?
Automations are available on the Free, Plus, and Premium plans. The number of automation actions per month depends on your plan. See the limits for your plan on our pricing page.
To add or change this action, you need to be a workspace admin of the workspace the database app is in. You also need
Full accessto every database app the automation uses.
Add the Perform calculation action
To calculate a value in an automation:
Open the database app and click the
Create and view automationsicon at the top.Open an automation, or select
New automationand choose a trigger.Select
Add action→Perform calculation. In the list, it readsPerform a calculation in JavaScript and save the result in a variable for later use.Write your code in the code editor, which starts with the placeholder
Code.Select
Calculation resultto rename the variable, for example toFollow-up date.Select
Save changes.
Perform calculation settings
The action reads Perform a calculation and save result in Calculation result. Below it is the code editor.
The code editor: where you write the code. It has autocompletion and shows which variables are available. Start typing, then press
enterto accept a suggestion.Calculation result: the name of the variable that holds the result. Select it to rename it.Perform calculationsaves one result. Its variable has no type.Open in fullscreen: opens the editor in full screen. Usecmd/ctrl+enterto open and close it.
Click ••• next to the action to open its Settings: Continue on error, Deactivate, Turn into, Duplicate, and Remove. Learn more about action settings →
Write the code
The code can use values from the record and from earlier actions. The editor suggests their names as you type. Some examples:
task_field_estimation_days_decimal_value * 8
task_field_estimation_days_decimal_value < 100 ? task_field_estimation_days_decimal_value : 100
(lead_field_first_name_value + ' ' + lead_field_last_name_value).trim()
Field values are named after the record name of the database app and the field, for example lead_field_email_email for an email field or task_field_estimation_days_decimal_value for a number field. The editor suggests the exact names as you type.
Operators like && and ||, the conditional operator ? :, and functions such as Math.round all work. You can also use the same libraries as in Execute script, for example lodash, moment, date_fns, date_fns_tz, uuid, and striptags. Learn more about the libraries →
Write more complex code
You can write several statements, including if and loops. The value of the last statement is saved as the result:
let total = 0;
for (const n of [1, 2, 3]) {
total += n;
}
total
If you want to use return, wrap your code in a function that you call right away:
(function () {
const net = invoice_field_net_decimal_value;
const rate = invoice_field_tax_rate_decimal_value / 100;
function round2(value) {
return Math.round(value * 100) / 100;
}
return round2(net * (1 + rate));
})();To save an object, wrap it in parentheses so that it isn't read as a block:
({ net: invoice_field_net_decimal_value, tax: invoice_field_net_decimal_value * 0.19 })
Use the result
In later actions, type @ to open the list of variables, then select the result variable, for example in the field of Update record or in the message of Send email. The variable name is saved in the form var_<name>: Calculation result becomes var_calculation_result. Use this name inside later scripts and calculations.
A variable is available to every action after the one that makes it. It isn't limited to a block.
If two variables have the same name, the later one overwrites the earlier one.
Use letters and numbers in names. Characters other than letters, numbers,
_, and$become_in the variable name.
Let's look at an example
Let's say your sales team wants to follow up two weeks after a quote is sent. Here's how:
Set the trigger to
When a record is updatedand select the fieldStatus.Add the filter
StatusHas changed toQuote sent.Select
Add action→Perform calculation.Enter this code:
date_fns.format(date_fns.addDays(new Date(current_date), 14), 'yyyy-MM-dd')Rename
Calculation resulttoFollow-up date.Select
Add action→Update recordand insertFollow-up dateinto the date fieldFollow up on.Select
Save changes.
The next time a quote is sent, the follow-up date is set to 14 days later. Automations run in UTC, so check the new date on one record before you rely on it. Learn more about dates and time zones →
For more snippets, see Automation script examples →.
Perform calculation errors
Illegal return statementmeans the code usesreturnoutside a function. Remove it, or wrap the code in a function, as in the examples on this page.If the result is empty, the variable names in your code might not match. Check the spelling, and open the autocomplete to see the names.
If a result looks wrong, the value might be text instead of a number. Convert it with
Number(...)first. Learn more about script examples →Tape doesn't check your code for mistakes when you save. It only marks values you inserted with
@that no longer exist, for example a deleted field. Other mistakes show up when the automation runs. Test with a simulated run.
Test, pause, or edit the Perform calculation action
Use
Simulated runto test without changing data. The run doesn't show the result ofPerform calculation. To check the value, insert the variable in a later step, for example inSend email, then selectShow previewandSimulate variables.Manual runruns the automation once. UnderRun on, select the records, then selectSave and start run.To print values while you test, use
console.loginExecute script. Learn more about debugging scripts →To change the code, open the automation, edit the code, then select
Save changes.
Learn more
FAQs
What's the difference between Perform calculation and Execute script?
What's the difference between Perform calculation and Execute script?
Perform calculation runs synchronous code and saves one result. Execute script runs longer code, supports await, can call web services and the Tape API, and can save several outputs.
Can I use if statements and loops?
Can I use if statements and loops?
Yes, you can write several statements, including if and loops. The value of the last statement is saved as the result, for example let total = 0; for (const n of [1, 2, 3]) { total += n; } total. Don't use return at the top level.
Can I use the result in a condition?
Can I use the result in a condition?
Yes, calculate the check first, then use the variable in Conditional actions. Open the ••• menu of a filter and select Turn into script filter, then enter a check like var_calculation_result > 100.
Why do I get the error Illegal return statement?
Why do I get the error Illegal return statement?
The code uses return at the top level. In Perform calculation, the value of the last statement is saved, so remove return. If you need return, wrap your code in a function and call it right away.
Do I need a calculation field instead?
Do I need a calculation field instead?
It depends on where you need the value. Both use JavaScript. A calculation field keeps its value on the record. Perform calculation keeps the value only for the rest of the run. Learn more about the calculation field →
Are you a developer using the Tape API for automations?
Are you a developer using the Tape API for automations?
The automation API has its own documentation for developers. Full reference: custom_variable in the developer docs →
Still have questions? Find answers and get help from community experts in the Tape community →
