11.1 - Do Before Insert Event
Last updated 20/05/2025
Five has several table events. All table events execute on the backend as they are directly interacting with your database.
In the Book Club application, the Do Before Update event will be used on the Book table which will execute before the updated record is committed into the database. The context contains two objects
When the Book table is updated to add a borrowing member, a record will be inserted in the BorrowHistory table with the borrow date and a notification will be sent using Five's
When the Book table is updated to remove a borrowing member, the record will be updated in the BorrowHistory table with the returned date.
Navigate to Functions
1. Click Logic in the menu.2. Click Functions in the sub-menu.

Add the SaveBookBeforeUpdate Function
The
This operation is calculated by inspecting the
When a book is borrowed for the first time
- Unique borrow HistoryKey (UUID)
- MemberKey of the borrower
- BookKey
- Current timestamp as the borrow date
The current user's address is retrieved from the iUser table using Five's
When a book is returned
2. Type SaveBookBeforeUpdate in the Function ID field.
3. Click in the Code field to open the Code Editor.

4. Copy the code block below and paste it over the template in the Code Editor.
function SaveBookBeforeUpdate(five, context, result) {
if (context.old.BorrowMemberKey === context.new.BorrowMemberKey){
return five.success();
}
if (context.old.BorrowMemberKey === '') {
// create new History record
let userResults = five.executeQuery("INSERT INTO BorrowHistory (BorrowHistoryKey, MemberKey, BookKey, Date) Values(?, ?, ?, ?)", 0, five.uuid(), context.new.BorrowMemberKey, context.new.BookKey, five.now());
if (userResults.isOk() === false) {
return five.createError(userResults);
}
const currentUserKey = five.currentUserKey();
userResults = five.executeQuery("SELECT AddressLine1, AddressLine2, Suburb, State, PostCode FROM iUser WHERE iUserKey = ?", 1, currentUserKey);
if (userResults.isOk() === false) {
return five.createError(userResults);
}
const AddressLine1 = userResults.records[0].AddressLine1;
const AddressLine2 = userResults.records[0].AddressLine2;
const Suburb = userResults.records[0].Suburb;
const State = userResults.records[0].State;
const PostCode = userResults.records[0].PostCode;
let address = AddressLine1;
if (AddressLine2) {
address += "," + AddressLine2;
}
address += `,${Suburb},${PostCode},${State}`;
const message = `Book is ready at collection point : https://maps.google.com?q=${address}`;
five.sendNotification(context.new.BorrowMemberKey, message);
} else if (context.new.BorrowMemberKey === '') {
// update history record that the book has been returned
const userResults = five.executeQuery("UPDATE BorrowHistory SET DateReturned = ? WHERE MemberKey = ? AND BookKey = ?", 0, five.now(), context.old.BorrowMemberKey, context.new.BookKey);
if (userResults.isOk() === false) {
return five.createError(userResults);
}
} else {
return five.createError(result, 'Book must be returned before being borrowed again.')
}
return five.success(result);
}
5. Click the Save button in the editor app bar.

6. Click the Save button in the form app bar.

Navigate to Tables
The
2. Click Tables in the sub-menu.

Attach the SaveBookBeforeUpdate Function
1. Select the Book record in the list.2. Click the Events tab.

3. Either click the Edit button in the form app bar or click directly in the Do Before Update field.

4. Click the lookup icon in the Do Before Update field and select SaveBookBeforeUpdate.

5. Click the Save button in the form app bar.
