In this article we will show how to use Event handlers in Vtiger 7 for us to write custom logic for after Save or Before save events.  We have given an example using potential module the same should be applicable for other VTiger modules.

There are two steps in:

  • Register event Handler 
  • Create a custom handler class and write our custom logic in handle event method.

Register Event Handler:

First Create a new php file with below code and change respective fields.

Vtiger_Event::register(‘<ModuleName>’, ‘vtiger.entity.aftersave/vtiger.entity.beforesave’, ‘<Handlerclass>’, ‘<HandlerFile>’);

Then run the above file in the browser, then you will find a row in database vtiger_eventhandler table. Cross verify by checking if it created the requisite entry.

Create a custom handler class and write our custom logic in handle event method:

Open Handler File and write below code.

class copyPotentialToContacts extends VTEventHandler
public function handleEvent($eventName, $data)
global $adb;
if ($eventName == ‘vtiger.entity.beforesave’) {
// Entity is about to be saved, take required action
if ($eventName == ‘vtiger.entity.aftersave’) {
// Entity is saved, take required action

->To get column fields of Form(in both beforesave/aftersave), write below code.

$columnFields = $data->getData();

->In the above $column Fields variable you will find all the fields filled in the form.


Modifying Contacts details after saving of Potential

-> In this example we are updating Contact details like Mobile Number, International Number, Secondary mobile number and Email from Potential.

->First get Potential details

$id = $entityData->getId();
$contactId = $columnFields[‘contact_id’];
$mobile = $columnFields[‘mobile’];
$internationalPhone = $columnFields[‘cf_1509’];
$SecondaryPhone = $columnFields[‘cf_1507’];
$Email = $columnFields[‘cf_1543’];

->Here $id is PotentialId and $contactid is a Contact which was associated to Potential.

->Then write sql query to update Contact details,

$query = "UPDATE vtiger_contactdetails SET ";
if (strlen($mobile) > 0) {
$query .= "mobile = ‘" .$mobile."’, salutation = ‘Event’";
if (strlen($internationalPhone) > 0) {
$query .= ", international_phone = ‘$internationalPhone’";
if (strlen($Email) > 0) {
$query .= ", email = " . "’" .$Email."’";
$query .= " where contactid=".$contactId;

->Updating Contacts module custom fields, we need to use “contactscf” table to update any custom field.

if (strlen($SecondaryPhone) > 0) {
$adb->pquery("Update vtiger_contactscf set cf_1511 = ‘$SecondaryPhone’ where contactid = ? ", array($contactId));