Do Before Update
Last updated 1/04/2026
Overview
The Do Before Update event is a server-side that executes just before an updated record is committed to the database. It runs during the update operation, giving you the opportunity to inspect, validate, or modify the data before the changes are saved.
This event provides access to two objects on the
- old- the existing record currently stored in the database
- new- the updated version of the record that is about to be saved
By comparing these two objects, you can control how updates are applied and enforce rules before the database is modified.
How it Works
When an existing record is updated:
- The Do Before Update event executes on the server.
- The oldobject contains the current values stored in the database.
- The newobject contains the updated values submitted for the record.
- The record has not yet been committed to the database.
- You can compare the oldandnewto detect changes.
- Any changes made to the newobject will be saved as part of the update.
- Returning an error will prevent the update from being committed.
Use Cases
- Validate changes before updating a record
- Prevent certain fields from being modified
- Enforce business rules based on changes between new and old values
- Automatically update audit fields such as modified timestamp or user IDs
- Trigger logic only when specific fields have changed
Example
This function runs before updating a customer record, compares the existing and updated values, checks if the email has changed and already exists in another record, prevents duplicate email by creating an error, and sets an updated timestamp on the record before the changes are committed.
function doBeforeUpdate(five, context, result) {
// The 'old' object is the record currently in the DB
const oldRecord = context.old;
// The 'new' object is the updated record that will be saved
const newRecord = context.new;
// If the email has changed, check for duplicates in other records
if (oldRecord.Email !== newRecord.Email) {
const query = `
SELECT COUNT(*) AS count
FROM Customers
WHERE Email = ? AND CustomerID != ?
`;
// Execute the query with the new email and exclude the current customer by ID
const queryResult = five.executeQuery(
query,
0,
newRecord.Email,
oldRecord.CustomerID
);
// If the email already exists for another customer, prevent the update
if (queryResult.recordCount() > 0) {
return five.createError(`The email "${newRecord.Email}" is already registered.`);
}
}
// Update the modified timestamp (ensure the field exists in your table)
newRecord.UpdatedAt = five.now();
return five.success();
}