Event Hook: add_beforesave()
in Merciglobal Cloud ERP
๐ Overview
The add_beforesave()
event hook in Merciglobal Cloud ERP allows developers to inject custom logic before a new record is saved to the database. It is typically used for validation, data manipulation, or conditional blocking of the save operation.
๐งฉ Syntax
function {tablename}_add_beforesave() {
global $data;
// Your custom logic here
return false; // return true to allow save, false to abort
}
๐ข Parameters
global $data
: This global variable contains the data that is about to be saved.
๐ Return Values
Return Value | Description |
---|---|
true |
Proceed with saving the record. |
false |
Cancel the save operation. |
๐ก Common Use Cases
- โ Validation: Ensure required fields meet certain criteria.
- โ Authorization: Verify if the user has permission to add the data.
- โ Data Cleanup: Modify fields (e.g., trimming whitespace).
- โ Block Save: Halt saving based on conditions.
๐งช Example: Prevent Save if Email is Missing
function customer_add_beforesave() {
global $data;
if (empty($data['email'])) {
msg("Email address is required.");
return false;
}
return true;
}
๐ ๏ธ Developer Tips
- ๐ Use
msg()
to inform users why saving was blocked. - ๐งพ Function name must strictly follow:
{tablename}_add_beforesave()
- ๐งช Test thoroughly in a staging environment.
โ Best Practices
- Keep the function lightweight and focused.
- Avoid complex or time-consuming operations.
- Use logs for debugging complex validations.
๐ Related Event Hooks
add_aftersave()
โ Executes after a successful save.edit_beforesave()
โ Applies logic before updating existing records.
๐ Conclusion
The add_beforesave()
hook is essential for enforcing data quality and business rules in Merciglobal Cloud ERP. Proper use ensures that only valid, authorized data enters your ERP, enhancing overall system integrity.
Master the hook. Elevate your ERP. ๐ก๏ธ๐