Back
afterUpdate()
Registers one or more callback methods that should be executed after an existing object has been updated in the database. This hook is ideal for performing follow-up tasks whenever a record changes — such as logging, cache invalidation, or sending notifications about updates.
1. Simple logging
afterUpdate("logUpdate");
function logUpdate() {
writeLog("Record ##this.id## was updated at #now()#");
}
2. Trigger an email when a specific field changes
afterUpdate("notifyEmailChange");
function notifyEmailChange() {
if (this.hasChanged("email")) {
sendEmail(
to=this.email,
subject="Your email was updated",
body="Hi #this.firstName#, your email address has been changed."
);
}
}
3. Multiple callbacks
afterUpdate("logUpdate,notifyEmailChange");
4. Example in Order.cfc
component extends="Model" {
function config() {
afterUpdate("updateInventory,sendUpdateNotification");
}
function updateInventory() {
Inventory.adjustStock(this.productId, -this.quantity);
}
function sendUpdateNotification() {
EmailService.sendOrderUpdate(this.id);
}
}
Copied!