In Vtiger CRM, beforesave
and beforesave.modifiable
are event hooks used to execute custom logic before saving a record. Here’s a detailed explanation of both:
beforesave
The beforesave
event is triggered just before a record is saved. It allows you to execute custom code right before the save operation. However, at this point, the record is not yet marked as modifiable, meaning you can perform actions based on the record’s data but cannot alter the data itself.
Usage Example:
class CustomModuleHandler extends VTEventHandler {
public function handleEvent($eventName, $data) {
if ($eventName == 'beforesave') {
// Custom logic here
// Access record data using $data->get('fieldname')
}
}
}
beforesave.modifiable
The beforesave.modifiable
event is similar to beforesave
, but with a crucial difference: it allows modifications to the record data before saving. This means you can update field values, perform validations, or any other data alterations before the record is actually saved to the database.
Usage Example:
class CustomModuleHandler extends VTEventHandler {
public function handleEvent($eventName, $data) {
if ($eventName == 'beforesave.modifiable') {
// Custom logic here
// Access and modify record data using $data->set('fieldname', 'new value')
$data->set('customfield', 'New Value');
}
}
}
Key Differences
- Modifiability:
- beforesave: You can read the record data but cannot modify it.
- beforesave.modifiable: You can both read and modify the record data.
2. Use Cases:
- beforesave: Useful for logging, condition checking, or any operation that doesn’t require altering the record data.
- beforesave.modifiable: Useful for data validation, field value updates, or any operation that requires modifying the record before it is saved.
Implementation Steps
- Create Event Handler File:
- Create a PHP file for your custom event handler, for example,
modules/CustomModule/handlers/CustomModuleHandler.php
.
2. Register Event Handler:
- Register your event handler in the
vtiger_eventhandlers
table or through the module’s manifest file.
Event Handler Example:
class CustomModuleHandler extends VTEventHandler {
public function handleEvent($eventName, $data) {
if ($eventName == 'beforesave') {
// Logic for beforesave event
}
if ($eventName == 'beforesave.modifiable') {
// Logic for beforesave.modifiable event
$data->set('customfield', 'New Value');
}
}
}
- Update manifest.xml (Optional):
- Add event handler registration in the
manifest.xml
file of your module.
<events>
<event>
<eventname>beforesave</eventname>
<handler_class>CustomModuleHandler</handler_class>
<handler_path>modules/CustomModule/handlers/CustomModuleHandler.php</handler_path>
<condition>instanceof CustomModule</condition>
</event>
<event>
<eventname>beforesave.modifiable</eventname>
<handler_class>CustomModuleHandler</handler_class>
<handler_path>modules/CustomModule/handlers/CustomModuleHandler.php</handler_path>
<condition>instanceof CustomModule</condition>
</event>
</events>
By using these event hooks appropriately, you can customize the behavior of your Vtiger CRM system to meet specific business needs.
Conclusion
Knowing about all the variations among beforesave.modifiable and beforsave events completely hooks in vTiger CRM is important for modifying your system of CRM to meet particular requirements of the business. By using all these hooks successfully, you can easily improve the working of your CRM. Discovering the best Vtiger hosting solutions can offer the required help and assets to guarantee a unified execution.