{"id":8693,"date":"2024-07-12T07:14:06","date_gmt":"2024-07-12T07:14:06","guid":{"rendered":"https:\/\/www.infinitivehost.com\/knowledge-base\/?p=8693"},"modified":"2024-08-23T08:22:46","modified_gmt":"2024-08-23T08:22:46","slug":"beforesave-vs-beforesave-modifiable-key-differences-explained","status":"publish","type":"post","link":"https:\/\/www.infinitivehost.com\/knowledge-base\/beforesave-vs-beforesave-modifiable-key-differences-explained\/","title":{"rendered":"beforesave vs beforesave.modifiable: Key Differences Explained"},"content":{"rendered":"<div class='epvc-post-count'><span class='epvc-eye'><\/span>  <span class=\"epvc-count\"> 1,117<\/span><span class='epvc-label'> Views<\/span><\/div>\n<p>In Vtiger CRM, <code>beforesave<\/code> and <code>beforesave.modifiable<\/code> are event hooks used to execute custom logic before saving a record. Here\u2019s a detailed explanation of both:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><code>beforesave<\/code><\/h3>\n\n\n\n<p>The <code>beforesave<\/code> 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&#8217;s data but cannot alter the data itself.<\/p>\n\n\n\n<p><strong>Usage Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">class CustomModuleHandler extends VTEventHandler {\n    public function handleEvent($eventName, $data) {\n        if ($eventName == 'beforesave') {\n            \/\/ Custom logic here\n            \/\/ Access record data using $data-&gt;get('fieldname')\n        }\n    }\n}<\/mark><\/code><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><code>beforesave.modifiable<\/code><\/h3>\n\n\n\n<p>The <code>beforesave.modifiable<\/code> event is similar to <code>beforesave<\/code>, 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.<\/p>\n\n\n\n<p><strong>Usage Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">class CustomModuleHandler extends VTEventHandler {\n    public function handleEvent($eventName, $data) {\n        if ($eventName == 'beforesave.modifiable') {\n            \/\/ Custom logic here\n            \/\/ Access and modify record data using $data-&gt;set('fieldname', 'new value')\n            $data-&gt;set('customfield', 'New Value');\n        }\n    }\n}<\/mark><\/code><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Key Differences<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Modifiability:<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>beforesave<\/strong>: You can read the record data but cannot modify it.<\/li>\n\n\n\n<li><strong>beforesave.modifiable<\/strong>: You can both read and modify the record data.<\/li>\n<\/ul>\n\n\n\n<p>    2. <strong>Use Cases:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>beforesave<\/strong>: Useful for logging, condition checking, or any operation that doesn\u2019t require altering the record data.<\/li>\n\n\n\n<li><strong>beforesave.modifiable<\/strong>: Useful for data validation, field value updates, or any operation that requires modifying the record before it is saved.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Implementation Steps<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Create Event Handler File:<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Create a PHP file for your custom event handler, for example, <code>modules\/CustomModule\/handlers\/CustomModuleHandler.php<\/code>.<\/li>\n<\/ul>\n\n\n\n<p>    2. <strong>Register Event Handler:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Register your event handler in the <code>vtiger_eventhandlers<\/code> table or through the module\u2019s manifest file.<\/li>\n<\/ul>\n\n\n\n<p><strong>Event Handler Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">class CustomModuleHandler extends VTEventHandler {\n    public function handleEvent($eventName, $data) {\n        if ($eventName == 'beforesave') {\n            \/\/ Logic for beforesave event\n        }\n        if ($eventName == 'beforesave.modifiable') {\n            \/\/ Logic for beforesave.modifiable event\n            $data-&gt;set('customfield', 'New Value');\n        }\n    }\n}<\/mark><\/code><\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Update manifest.xml (Optional):<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Add event handler registration in the <code>manifest.xml<\/code> file of your module.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">&lt;events&gt;\n    &lt;event&gt;\n        &lt;eventname&gt;beforesave&lt;\/eventname&gt;\n        &lt;handler_class&gt;CustomModuleHandler&lt;\/handler_class&gt;\n        &lt;handler_path&gt;modules\/CustomModule\/handlers\/CustomModuleHandler.php&lt;\/handler_path&gt;\n        &lt;condition&gt;instanceof CustomModule&lt;\/condition&gt;\n    &lt;\/event&gt;\n    &lt;event&gt;\n        &lt;eventname&gt;beforesave.modifiable&lt;\/eventname&gt;\n        &lt;handler_class&gt;CustomModuleHandler&lt;\/handler_class&gt;\n        &lt;handler_path&gt;modules\/CustomModule\/handlers\/CustomModuleHandler.php&lt;\/handler_path&gt;\n        &lt;condition&gt;instanceof CustomModule&lt;\/condition&gt;\n    &lt;\/event&gt;\n&lt;\/events&gt;<\/mark><\/code><\/code><\/pre>\n\n\n\n<p>By using these event hooks appropriately, you can customize the behavior of your Vtiger CRM system to meet specific business needs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>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 <a href=\"https:\/\/www.infinitivehost.com\/managed-vtiger-solutions\"><mark style=\"background-color:#8ed1fc\" class=\"has-inline-color has-black-color\"><strong>best Vtiger hosting solutions<\/strong><\/mark><\/a> can offer the required help and assets to guarantee a unified execution.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1,117 Views In Vtiger CRM, beforesave and beforesave.modifiable are event hooks used to execute custom logic before saving a record. Here\u2019s 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[204],"tags":[],"class_list":["post-8693","post","type-post","status-publish","format-standard","hentry","category-vtiger-solutions"],"_links":{"self":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts\/8693","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/comments?post=8693"}],"version-history":[{"count":2,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts\/8693\/revisions"}],"predecessor-version":[{"id":8897,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts\/8693\/revisions\/8897"}],"wp:attachment":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/media?parent=8693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/categories?post=8693"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/tags?post=8693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}