{"id":8674,"date":"2024-07-05T06:41:56","date_gmt":"2024-07-05T06:41:56","guid":{"rendered":"https:\/\/www.infinitivehost.com\/knowledge-base\/?p=8674"},"modified":"2024-08-23T07:57:33","modified_gmt":"2024-08-23T07:57:33","slug":"auto-create-calendar-events-from-custom-modules-in-vtiger","status":"publish","type":"post","link":"https:\/\/www.infinitivehost.com\/knowledge-base\/auto-create-calendar-events-from-custom-modules-in-vtiger\/","title":{"rendered":"Auto-Create Calendar Events from Custom Modules in Vtiger"},"content":{"rendered":"<div class='epvc-post-count'><span class='epvc-eye'><\/span>  <span class=\"epvc-count\"> 1,650<\/span><span class='epvc-label'> Views<\/span><\/div>\n<p>Creating a record in a custom modules and automatically generating a corresponding event in the calendar in vTiger CRM involves using custom workflows or code hooks. Below, I\u2019ll outline both methods: using the vTiger built-in workflow system and implementing custom PHP code.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Method 1: Using vTiger Workflow System<\/h3>\n\n\n\n<p>vTiger&#8217;s workflow system can automate actions based on specific conditions. Here\u2019s how you can set it up to create an event whenever a new record is created in a custom module:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Step-by-Step Guide:<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Access the Workflow Module<\/strong>:<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Navigate to <code>Settings &gt; CRM Settings &gt; Automation &gt; Workflows<\/code>.<\/li>\n<\/ul>\n\n\n\n<p>     2. <strong>Create a New Workflow<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Click on the <code>+ Add Workflow<\/code> button.<\/li>\n\n\n\n<li>Select your custom module from the list.<\/li>\n\n\n\n<li>Set the description, for example, &#8220;Create Event on New Record&#8221;.<\/li>\n\n\n\n<li>Choose the trigger to be <code>On Create<\/code> so the workflow runs when a new record is added.<\/li>\n<\/ul>\n\n\n\n<p>    3. <strong>Set Workflow Conditions (if any)<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You can set conditions based on the fields of the custom module if you want the event creation to be conditional.<\/li>\n<\/ul>\n\n\n\n<p>    4. <strong>Add Action to Create Event<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Click on <code>Add Action<\/code>.<\/li>\n\n\n\n<li>Choose <code>Create Record<\/code>.<\/li>\n\n\n\n<li>From the dropdown, select the <code>Events<\/code> module (or <code>Calendar<\/code> module if it\u2019s named differently).<\/li>\n\n\n\n<li>Fill in the necessary fields for the event. You can map the custom module fields to the event fields. For instance:\n<ul class=\"wp-block-list\">\n<li>Subject: <code>[Custom Module Name] - {$CustomModuleName} created<\/code><\/li>\n\n\n\n<li>Start Date &amp; Time: Set it to the current date and time or a specific time.<\/li>\n\n\n\n<li>End Date &amp; Time: Set a duration from the start date.<\/li>\n\n\n\n<li>Status: Choose a default status like &#8216;Planned&#8217;.<\/li>\n\n\n\n<li>Other fields as necessary.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>    5. <strong>Save the Workflow<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Review the setup and click <code>Save<\/code>.<\/li>\n<\/ul>\n\n\n\n<p>This workflow will now automatically create an event in the calendar whenever a new record is created in your custom module.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Method 2: Custom PHP Code Hook<\/h3>\n\n\n\n<p>For more flexibility or complex logic, you can use custom PHP code. You\u2019ll need to add a handler that triggers when a record is created in your custom module.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Step-by-Step Guide:<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Locate the Module&#8217;s Save Handler<\/strong>:<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Go to <code>modules\/YourCustomModuleName\/handlers\/Save.php<\/code>. If it doesn\u2019t exist, you might need to create it or modify the existing save handler in your module&#8217;s model.<\/li>\n<\/ul>\n\n\n\n<p>    2. <strong>Create a Handler Class<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Add a handler method in the save handler to execute after a record is saved. Here\u2019s a basic example:<\/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\">class YourCustomModuleName_Save_Handler extends VTEventHandler {\n       public function handleEvent($eventName, $entityData) {\n           if($eventName == 'vtiger.entity.aftersave') {\n               $this-&gt;createEvent($entityData);\n           }\n       }\n\n       public function createEvent($entityData) {\n           $recordId = $entityData-&gt;getId();\n           $moduleName = $entityData-&gt;getModuleName();\n\n           if($moduleName == 'YourCustomModuleName') {\n               \/\/ Get record data\n               $recordModel = Vtiger_Record_Model::getInstanceById($recordId, $moduleName);\n               $subject = $recordModel-&gt;get('field_name'); \/\/ Replace 'field_name' with an actual field name from your module\n\n               \/\/ Create a new Event\n               $eventModuleModel = Vtiger_Module_Model::getInstance('Events');\n               $eventRecordModel = Vtiger_Record_Model::getCleanInstance('Events');\n\n               $eventRecordModel-&gt;set('subject', $subject);\n               $eventRecordModel-&gt;set('date_start', date('Y-m-d'));\n               $eventRecordModel-&gt;set('due_date', date('Y-m-d', strtotime('+1 hour')));\n               $eventRecordModel-&gt;set('time_start', date('H:i:s'));\n               $eventRecordModel-&gt;set('time_end', date('H:i:s', strtotime('+1 hour')));\n               $eventRecordModel-&gt;set('eventstatus', 'Planned'); \/\/ Set the default status\n               $eventRecordModel-&gt;set('activitytype', 'Task'); \/\/ or 'Meeting'\n               $eventRecordModel-&gt;save();\n\n               \/\/ Link the event to the custom module record\n               $relationModel = Vtiger_Relation_Model::getInstance($recordModel, $eventModuleModel);\n               $relationModel-&gt;addRelation($recordId, $eventRecordModel-&gt;getId());\n           }\n       }\n   }<\/mark><\/code><\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Register the Event Handler<\/strong>:<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Register the handler in <code>vtiger_eventhandlers<\/code> table so vTiger knows to call it. You can insert this via SQL or use an existing method to insert: <code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">INSERT INTO vtiger_eventhandlers (event_name, handler_class, is_active) VALUES ('vtiger.entity.aftersave', 'modules\/YourCustomModuleName\/handlers\/Save.php', 1);<\/mark><\/code><\/li>\n<\/ul>\n\n\n\n<p>    4. <strong>Deploy and Test<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Upload or save your changes to the server.<\/li>\n\n\n\n<li>Test by creating a new record in your custom module and verify that an event is created in the calendar.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Final Considerations:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Permissions<\/strong>: Ensure the user creating the record has permission to create events.<\/li>\n\n\n\n<li><strong>Debugging<\/strong>: Check vTiger logs for errors if the event creation doesn\u2019t work as expected.<\/li>\n\n\n\n<li><strong>Dependencies<\/strong>: If your custom module or event module relies on other modules or configurations, make sure they are correctly set up.<\/li>\n<\/ul>\n\n\n\n<p>By following these steps, you can automatically create calendar events when records are added to your custom module in vTiger CRM.<\/p>\n\n\n\n<p><strong>Conclusion<\/strong><\/p>\n\n\n\n<p>Making a specific record in any custom module and necessarily producing a consistent event in the case of calendar in 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 solution<\/strong><\/mark><\/a> consists of utilizing custom flow of work or sometimes code hooks. Above highlighted both approaches: utilizing the vTiger in-built workflow system and applying proper PHP code. By obeying all the above-mentioned steps, you can easily develop calendar events when all records are automatically added to your module.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1,650 Views Creating a record in a custom modules and automatically generating a corresponding event in the calendar in vTiger CRM involves using custom workflows or code hooks. Below, I\u2019ll outline both methods: using the vTiger built-in workflow system and implementing custom PHP code. Method 1: Using vTiger Workflow System vTiger&#8217;s workflow system can automate [&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-8674","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\/8674","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=8674"}],"version-history":[{"count":3,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts\/8674\/revisions"}],"predecessor-version":[{"id":8881,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts\/8674\/revisions\/8881"}],"wp:attachment":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/media?parent=8674"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/categories?post=8674"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/tags?post=8674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}