Skip to content

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.

  • 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. ๐Ÿ›ก๏ธ๐Ÿš€