Skip to main content

Do Before Insert

Last updated 31/03/2026

Overview

The Do Before Insert event is a server-side event that executes just before a new record is committed to the database. It runs during the insert operation, giving you the opportunity to inspect or modify the data before it is saved. This event provides access to a

new
object on the
context
parameter, which contains the record being inserted with the
databaseKey
and the
tableKey
where the record will be inserted.

How it Works

When a new record is created:

  1. The Do Before Insert event executes on the server.
  2. The
    new
    object contains all field values for the record being inserted.
  3. The record has not yet been committed to the database.
  4. Any changes made to the
    new
    object will be saved as part of the insert.

Use Cases

  • Validate and enforce business rules before saving a new record
  • Modify or set default values before the record is saved
  • Prevent invalid or incomplete data from being inserted
  • Automatically populate fields such as timestamps, statuses, or calculated fields

Example

This function runs before inserting a new customer record, checks if the email already exists using

, prevents duplicates by creating an error, and sets a creation timestamp.

JavaScript
Check if email already exists for a customer
function doBeforeInsert(five, context, result) {
// The 'new' object contains the record being inserted
const newRecord = context.new;

// Example: Check if the customer email already exists in the database
const query = `
SELECT COUNT(*) AS count
FROM Customers
WHERE Email = ?
`;

// Execute the query using executeQuery() with the email from the new record
const result = five.executeQuery(query, 0, newRecord.Email);

// If the email already exists, prevent the insert
if (result.recordCount() > 0) {
return five.createError(`The email "${newRecord.Email}" is already registered.`);
}

// Optionally, you can modify the record before insert
newRecord.CreatedAt = five.now(); // Automatically set the creation timestamp
return five.success();
}