Common Script Snippets

Short scripts for common tasks, ready to copy. Replace the property names (such as data.lastName) with the property names of your own components, found on each component's API tab.

Unless noted, these are Calculated Value scripts: open the component's Data tab and expand Calculated Value → JavaScript Value.

The signed-in user

For the user's name, a Text Field has a Current User option on its Data tab that needs no script. For other details, or to keep the first user's details, use the scripts below.

Fill in the user's email address or name

JavaScript
value = (user && user.data && user.data.email) || '';

For the name, use user.data.name. Which fields exist depends on your user resource.

Fill it in once and keep it

When a form passes through several people, a calculated value (and the Current User option) changes to whoever opened the form last. To record the person who first filled it in:

JavaScript
value = data.submittedBy || (user && user.data && user.data.email) || '';

Here submittedBy is the property name of the component the script is on.

Text

Convert to capital letters

JavaScript
value = (data.lastName || '').toUpperCase();

Remove extra spaces

JavaScript
value = (data.companyName || '').replace(/\s+/g, ' ').trim();

Numbers

Add two values safely

Treats empty fields as zero, so the result is never blank or NaN:

JavaScript
value = (Number(data.amount1) || 0) + (Number(data.amount2) || 0);

Round to two decimal places

JavaScript
value = Math.round((Number(data.subtotal) || 0) * 1.07 * 100) / 100;

Choices

Allow only one checkbox at a time

Use a Radio component instead of several checkboxes. It allows only one choice, needs no script, and is easier to maintain.

Fill a value based on a choice

JavaScript
var approvers = {
  finance: 'finance-approvals@example.com',
  hr: 'hr-approvals@example.com'
};
value = approvers[data.department] || '';