Skip to main content

Automation script examples

Copy-ready JavaScript for text, numbers, dates, and lists. Use the examples in Perform calculation or Execute script, and avoid the common traps.

Written by Leo

Script examples are short pieces of JavaScript that you can paste into Perform calculation or Execute script and adapt (each one takes values from a record and returns a result you can use in a later action). They're meant to help you format text, work out dates, and calculate numbers without starting from a blank editor.

You can use these examples for many things, including:

  • Cleaning up a name or an email address before it goes into an email.

  • Setting a due date that's a fixed number of days after another date.

Note:

  • Replace the variable names. The examples use names like lead_field_name_value. 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 invoice_field_amount_decimal_value for a number field. A multi line text field offers _unformatted_value (plain text) and _formatted_value (with HTML) instead. In your automation, the editor suggests the real names as you type.

  • Use the number value of number fields. For a number field, use the name that ends in _decimal_value. It holds a number, so you can calculate with it right away. Other values that look like numbers can still be text. Convert them with Number(...) before you calculate.

  • Dates are in UTC. Automations run in UTC, so convert dates when the time of day matters. See the date examples below.

The action Perform a calculation and save result in Calculation result with the example code // Round to two decimals and Math.round(expense_field_amount_decimal_value * 100) / 100

Who can use script examples?

  • 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 an automation, you need to be a workspace admin of the workspace the database app is in. You also need Full access to every database app the automation uses.

Where to use the examples

The examples work in two places:

  • In Perform calculation, the value of the last statement is saved in Calculation result. Paste an example as it is. You can also write several statements, including if and loops.

  • In Execute script, the code can have several lines. Assign the result to an output variable, for example var_script_output_1 = .... This action also allows await.

Both actions support lodash (or _), moment, date_fns, date_fns_tz, uuid, cheerio, striptags, minimatch, and jsonata. Learn more about Perform calculation → and Execute script action →

Text examples

To clean up, combine, and cut text. If you know the function from a spreadsheet:

  • uppercase: toUpperCase()

  • round: Math.round()

  • replace: replace()

  • today: current_date

// Remove spaces at the start and end
lead_field_name_value.trim()
// Full name from two fields
(lead_field_first_name_value + ' ' + lead_field_last_name_value).trim()
// Capitalize each word: "maple grove lane" becomes "Maple Grove Lane"
_.startCase(_.toLower(property_field_street_value))
// Initials: "Juliet Adams" becomes "JA"
lead_field_name_value.split(' ').map(function (part) { return part.charAt(0).toUpperCase(); }).join('')
// First 80 characters, with dots if the text is longer
task_field_description_unformatted_value.length > 80
? task_field_description_unformatted_value.slice(0, 80) + '...'
: task_field_description_unformatted_value
// Replace text
property_field_notes_value.replace(/\s+/g, ' ')
// Use a fallback when a field is empty
lead_field_company_value || 'No company'

To remove HTML from the formatted value of a multi line text field, use the striptags library: striptags(task_field_description_formatted_value).

Email address and regex examples

A regular expression, or regex, finds text that matches a pattern. Use match to get a part of the text, and test to check whether the pattern is there.

// Domain of an email address: "[email protected]" becomes "acme.com"
lead_field_email_email.split('@')[1]
// Address from a header: "Juliet Adams <[email protected]>" becomes "[email protected]"
(lead_field_sender_value.match(/<([^>]+)>/) || [null, lead_field_sender_value])[1]
// First invoice number in a text: "Re: INV-20481 is overdue" becomes "INV-20481"
(email_field_subject_value.match(/INV-\d{4,}/) || [''])[0]
// Digits only: "+1 (555) 010-2030" becomes "15550102030"
lead_field_phone_phone.replace(/\D/g, '')
// Does the text contain the word "legal"? (true or false)
/\blegal\b/i.test(task_field_description_unformatted_value)
// Is it a company address and not a free mail address? (true or false)
!/@(gmail|outlook|yahoo)\./i.test(lead_field_email_email)

The match call returns null when nothing matches, so the examples above use || [...] to return an empty value instead of an error.

Number examples

// Round to two decimals
Math.round(invoice_field_amount_decimal_value * 100) / 100
// Add 19 percent tax
Math.round(invoice_field_net_decimal_value * 1.19 * 100) / 100
// Percentage of a goal: 45 of 60 becomes 75
Math.round((deal_field_won_decimal_value / deal_field_goal_decimal_value) * 100)
// Never go above 100 (and never below 0)
Math.min(100, Math.max(0, task_field_progress_decimal_value))
// Text with two decimals: 1234.5 becomes "1234.50"
Number(invoice_field_amount_decimal_value).toFixed(2)
// Even or odd (true for even)
task_field_number_decimal_value % 2 === 0

Date examples

Date fields give you the date, for example 2026-11-09, and the time, for example 15:00:00, as separate values. The formatted value combines them without seconds, for example 2026-11-09 15:00. Current date & time uses 2026-11-09 15:00:00. A date without a time uses 2026-11-09. Use date_fns to work with them.

A date field produces variables like task_field_due_date_start_date (the date in local time) and task_field_due_date_start_date_utc (the same date in UTC), for a field Due date in a database app whose records are called Task.

// Format a date: "2026-11-09" becomes "November 9, 2026"
date_fns.format(new Date(task_field_due_date_start_date), 'MMMM d, yyyy')
// 14 days after today, as a date
date_fns.format(date_fns.addDays(new Date(current_date), 14), 'yyyy-MM-dd')
// Days between two dates
date_fns.differenceInCalendarDays(new Date(task_field_due_date_start_date), new Date(current_date))
// Weekday name: "Monday"
date_fns.format(new Date(task_field_due_date_start_date), 'EEEE')
// Only the year
date_fns.format(new Date(task_field_due_date_start_date), 'y')

Set a date with a time

Automations run in UTC. If you create a date with a time in code and save it to a field, Tape reads it as UTC, so the person sees a shifted time. Convert it with the time zone of the person first:

date_fns_tz.zonedTimeToUtc(new Date('2026-11-09 15:00'), 'Europe/Amsterdam')

Use the variables that end in _utc when you save dates to fields. Learn more about dates and time zones →

List examples

Fields with several values offer several variables, for example a list of labels and the same labels as one text. A Multiple select field Services has project_field_services_selected_labels_array (a list of labels) and project_field_services_selected_labels_string (the labels as one text). Use lodash to work with lists.

// Join values into one line: "Solar, Battery, Roof"
_.join(project_field_services_selected_labels_array, ', ')
// Remove duplicates
_.uniq(project_field_services_selected_labels_array)
// Count the values
project_field_services_selected_labels_array.length
// Sum a list of numbers from an earlier script output called Amounts
_.sum(_.map(var_amounts, Number))

Other fields with several values, such as relations, email addresses, and phone numbers, also offer a list and a text version. Use the one the editor suggests.

Condition examples

Calculations that return true or false work well as checks. Calculate the check with Perform calculation, then use the variable in Conditional actions. Open the ••• menu of a filter and select Turn into script filter. Then write a check that uses your variable, for example var_calculation_result === true.

// Large deal in the right region
deal_field_amount_decimal_value >= 1000 && deal_field_region_title === 'West'
// Due in the next 7 days
date_fns.differenceInCalendarDays(new Date(task_field_due_date_start_date), new Date(current_date)) <= 7

Examples for Execute script

These need several lines or await, so use them in Execute script.

The action Execute a script and save output in Script output 1, Script output 2, with the example code in the editor
// Read a web service and keep only the field you need
const { data } = await http.get('https://example.com/api/rates');
var_script_output_1 = data.rates.USD;
// Build one line per record for an email, from invoices you collected earlier
const lines = _.map(record_collection_invoices, function (record) {
return '- ' + record.title;
});
var_script_output_1 = lines.join('\n');

A list of collected records is named after the database app, for example record_collection_invoices for a database Invoices. It holds one entry per record, and each entry has the record's title.

// Stop the run with a message
if (!lead_field_email_email) {
throw new Error('Lead has no email address');
}
// Skip a call to an external service in a simulated run
if (current_workflow_is_simulation) {
console.info('Simulated run, no call made');
} else {
await http.get('https://example.com/api/notify');
}

Common traps

  • Text instead of numbers. '5' + 3 is '53'. Use Number('5') + 3, which is 8.

  • Empty fields. An empty field can be null. Calling a function on null fails the run. Use (value || '') first.

  • Decimal math. 0.1 + 0.2 isn't exactly 0.3. Round the result with Math.round(value * 100) / 100.

  • Comparing with ==. Use ===, which doesn't turn '1' into 1 on the way.

  • Dates without a time zone. A date string has no time zone, so it's read as UTC. Convert it before you save.

  • return in Perform calculation. The value of the last statement is saved, so a return at the top level fails the run. Use a function that you call right away.

  • Syntax mistakes. Tape doesn't check your code for mistakes when you save. It only marks values you inserted with @ that no longer exist. Test with Simulated run.

Test, pause, or edit script examples

  • Paste an example into the action, then select Simulated run. It doesn't change data. The run doesn't show the result of Perform calculation, so insert the variable in a later step, or log it in Execute script. Learn more about testing automations →

  • To find out what your code returns, use console.info(...) in Execute script.

  • To change the code, open the automation, edit it, then select Save changes.

Learn more

FAQs

Which language are the examples written in?

JavaScript. Both Perform calculation and Execute script use it.

Why does my example return nothing or fail?

The most common causes are:

  • The variable name doesn't match. Use the names that the editor suggests.

  • The field is empty, so the code calls a function on an empty value.

  • The code uses return outside a function in Perform calculation.

How do I add a link to the record in an email?

You don't need a script. In Send email, insert Record URL. Learn more about sending emails →

Can I use a library that isn't in the list?

No. You can use lodash, moment, date_fns, date_fns_tz, uuid, cheerio, striptags, minimatch, and jsonata. If you need another one, submit a feature request in the Tape community →.

Can I test a script on a real record without changing it?

Yes, start a Simulated run. It skips filters and doesn't change data. Calls to other web services with http in Execute script still happen, so use current_workflow_is_simulation to skip them. Learn more about testing automations →

Are you a developer using the Tape API for automations?

The automation API has its own documentation for developers. Full reference: custom_variable →

Still have questions? Find answers and get help from community experts in the Tape community →

Did this answer your question?