Event Hook: add_beforesave()
in MerciGlobal Cloud ERP¶
Overview¶
The add_beforesave()
event hook is a powerful feature in MerciGlobal Cloud ERP that allows developers to execute custom logic right before a new record is saved into the database for a specific module (table).
This function is used to validate, manipulate, or halt the saving of data before it persists in the system.
Syntax¶
function {tablename|_add_beforesave() {
global $data;
// Custom logic here
return false; // prevent save if necessary
}
Parameters¶
global $data
: This global variable holds the data that is about to be saved.
Return Values¶
Return Value | Description |
---|---|
true |
Continue with saving the record. |
false |
Abort the save operation. |
Use Cases¶
- ✅ Data validation: Ensure all required fields are set correctly.
- ✅ Custom authorization: Check if the current user has the right permissions.
- ✅ Auto-correction: Modify or sanitize incoming data.
- ✅ Abort save: Prevent saving under certain conditions.
Example: Prevent Saving When Field is Empty¶
function customer_add_beforesave() {
global $data;
if (empty($data['email'])) {
msg("Email address is required.");
return false; // prevent save
}
return true; // continue saving
}
Developer Tips¶
- 🔐 Use
msg()
function to display feedback to the user. - 🛠 Ensure your function is named with the exact format:
{tablename}_add_beforesave()
- 🧪 Always test with real module data in a staging environment before deploying to live.
Best Practices¶
- Keep logic concise and focused.
- Avoid heavy processing or DB calls in this hook.
- Use logging if you're debugging unexpected behavior.
Related Events¶
add_aftersave()
– Triggered after the record is saved.edit_beforesave()
– Used before saving edits to an existing record.
Conclusion¶
The add_beforesave()
event in MerciGlobal Cloud ERP helps maintain data integrity and apply custom logic dynamically. It's a must-know tool for any Merci module developer.
Use it wisely to ensure only valid and necessary data is recorded into your ERP system. 🚀