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’ll outline both methods: using the vTiger built-in workflow system and implementing custom PHP code.
Method 1: Using vTiger Workflow System
vTiger’s workflow system can automate actions based on specific conditions. Here’s how you can set it up to create an event whenever a new record is created in a custom module:
Step-by-Step Guide:
- Access the Workflow Module:
- Navigate to
Settings > CRM Settings > Automation > Workflows
.
2. Create a New Workflow:
- Click on the
+ Add Workflow
button. - Select your custom module from the list.
- Set the description, for example, “Create Event on New Record”.
- Choose the trigger to be
On Create
so the workflow runs when a new record is added.
3. Set Workflow Conditions (if any):
- You can set conditions based on the fields of the custom module if you want the event creation to be conditional.
4. Add Action to Create Event:
- Click on
Add Action
. - Choose
Create Record
. - From the dropdown, select the
Events
module (orCalendar
module if it’s named differently). - Fill in the necessary fields for the event. You can map the custom module fields to the event fields. For instance:
- Subject:
[Custom Module Name] - {$CustomModuleName} created
- Start Date & Time: Set it to the current date and time or a specific time.
- End Date & Time: Set a duration from the start date.
- Status: Choose a default status like ‘Planned’.
- Other fields as necessary.
- Subject:
5. Save the Workflow:
- Review the setup and click
Save
.
This workflow will now automatically create an event in the calendar whenever a new record is created in your custom module.
Method 2: Custom PHP Code Hook
For more flexibility or complex logic, you can use custom PHP code. You’ll need to add a handler that triggers when a record is created in your custom module.
Step-by-Step Guide:
- Locate the Module’s Save Handler:
- Go to
modules/YourCustomModuleName/handlers/Save.php
. If it doesn’t exist, you might need to create it or modify the existing save handler in your module’s model.
2. Create a Handler Class:
- Add a handler method in the save handler to execute after a record is saved. Here’s a basic example:
class YourCustomModuleName_Save_Handler extends VTEventHandler {
public function handleEvent($eventName, $entityData) {
if($eventName == 'vtiger.entity.aftersave') {
$this->createEvent($entityData);
}
}
public function createEvent($entityData) {
$recordId = $entityData->getId();
$moduleName = $entityData->getModuleName();
if($moduleName == 'YourCustomModuleName') {
// Get record data
$recordModel = Vtiger_Record_Model::getInstanceById($recordId, $moduleName);
$subject = $recordModel->get('field_name'); // Replace 'field_name' with an actual field name from your module
// Create a new Event
$eventModuleModel = Vtiger_Module_Model::getInstance('Events');
$eventRecordModel = Vtiger_Record_Model::getCleanInstance('Events');
$eventRecordModel->set('subject', $subject);
$eventRecordModel->set('date_start', date('Y-m-d'));
$eventRecordModel->set('due_date', date('Y-m-d', strtotime('+1 hour')));
$eventRecordModel->set('time_start', date('H:i:s'));
$eventRecordModel->set('time_end', date('H:i:s', strtotime('+1 hour')));
$eventRecordModel->set('eventstatus', 'Planned'); // Set the default status
$eventRecordModel->set('activitytype', 'Task'); // or 'Meeting'
$eventRecordModel->save();
// Link the event to the custom module record
$relationModel = Vtiger_Relation_Model::getInstance($recordModel, $eventModuleModel);
$relationModel->addRelation($recordId, $eventRecordModel->getId());
}
}
}
- Register the Event Handler:
- Register the handler in
vtiger_eventhandlers
table so vTiger knows to call it. You can insert this via SQL or use an existing method to insert:INSERT INTO vtiger_eventhandlers (event_name, handler_class, is_active) VALUES ('vtiger.entity.aftersave', 'modules/YourCustomModuleName/handlers/Save.php', 1);
4. Deploy and Test:
- Upload or save your changes to the server.
- Test by creating a new record in your custom module and verify that an event is created in the calendar.
Final Considerations:
- Permissions: Ensure the user creating the record has permission to create events.
- Debugging: Check vTiger logs for errors if the event creation doesn’t work as expected.
- Dependencies: If your custom module or event module relies on other modules or configurations, make sure they are correctly set up.
By following these steps, you can automatically create calendar events when records are added to your custom module in vTiger CRM.
Conclusion
Making a specific record in any custom module and necessarily producing a consistent event in the case of calendar in the best Vtiger hosting solution 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.