How to create vTiger CRM Event Handler

Introduction:

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.

How to register an Event Handler

Here method using with PHP code, First Create a new php file with below code and change respective fields. File name create_reservation_handler.php

registerHandler(‘vtiger.entity.aftersave’, ‘modules/’.$module.’/’.$module.’ItemsHandler.php’, $module.’ItemsHandler’);

echo ‘Event registered Successfully!.’;
?>

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

Hoe to use custom Event handler and write our custom logic in handle event method:

Go to the modules DIR /modules/, Create event handler file which is registered handler QuotesItemsHandler.php.

Open Handler File and write below code.

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

$columnFields = $data->getData();

$column Fields variable you will find all the fields filled in the form.

Example:

Select, add and update Quote items details after saving of Quotes

getModuleName();
if ($moduleName == ‘Quotes’) {
$reservID = $entityData->getId();
if(($entityData->get(‘cf_4315’) == ‘Completed’ || $entityData->get(‘cf_4315’) == ‘Canceled’ )) {
$Query = $adb->pquery(“SELECT id,productid FROM `vtiger_inventoryproductrel` WHERE id = “.$reservID, array());
$totl = $adb->num_rows($soQuery);
if ($totl > 0) {
for ($i=0; $i<$totl; $i++) { $productid = $adb->query_result($Query, $i, ‘productid’);
$adb->pquery(“UPDATE `vtiger_productcf` SET `cf_799`= ‘Available’ WHERE productid = “.$productid, array());
}
}
}elseif ($entityData->get(‘cf_4315’) == ‘Reserved’) {
$Query = $adb->pquery(“SELECT id,productid FROM `vtiger_inventoryproductrel` WHERE id = “.$reservID, array());
$totl = $adb->num_rows($soQuery);
if ($totl > 0) {
for ($i=0; $i<$totl; $i++) { $productid = $adb->query_result($Query, $i, ‘productid’);
$adb->pquery(“UPDATE `vtiger_productcf` SET `cf_799`= ‘Available’ WHERE productid = “.$productid, array());
}
}
}

}
}
}
}

?>

Enjoy!