Remove related records through webservices in Vtiger CRM

Remove related records through webservices in Vtiger CRM

Often in the process of developing APIs with Vtiger CRM, I had to deal with one very unpleasant situation - we delete any record from CRM, while related records are not deleted. They have to be removed separately through additional requests. And this makes the process of deleting all records very long and hard to implement.

So I wrote a separate function to delete all related records. The function is called vtws_delete_related and takes two parameters - the ID of the parent and the ID of the child. As the third parameter comes the User model to control access rights.

The function looks like this (you can put it in the include/Webservices/DeleteRelatedRecords.php file):


function vtws_delete_related($sourceRecordId, $relatedRecordId, $user = false)


global $log,$adb;


//Get source record id

list($moduleSourceId, $elementSourceId) = vtws_getIdComponents($sourceRecordId);

$webserviceObject = VtigerWebserviceObject::fromId($adb, $moduleSourceId);


//Get instanes handlers

$handlerPath = $webserviceObject->getHandlerPath();

$handlerClass = $webserviceObject->getHandlerClass();


require_once $handlerPath;

$handler = new $handlerClass($webserviceObject, $user, $adb, $log);

$meta = $handler->getMeta();


//Get objet entity name from record Id.

$sourceModuleName = $meta->getObjectEntityName($sourceRecordId);


//Get module permssion for user.

$types = vtws_listtypes(null, $user);


//Check permissions for user

if (! in_array($sourceModuleName, $types['types'])) {

throw new WebServiceException(WebServiceErrorCode::$ACCESSDENIED, 'Permission to perform the operation is denied');



//Check equal entites

if ($sourceModuleName !== $webserviceObject->getEntityName()) {

throw new WebServiceException(WebServiceErrorCode::$INVALIDID, 'Id specified is incorrect');



if (! $meta->hasPermission(EntityMeta::$UPDATE, $sourceRecordId)) {

throw new WebServiceException(WebServiceErrorCode::$ACCESSDENIED, 'Permission to read given object is denied');



//Check exist record id

if (! $meta->exists($elementSourceId)) {

throw new WebServiceException(WebServiceErrorCode::$RECORDNOTFOUND, 'Record you are trying to access is not found');



//Check record access

if ($meta->hasWriteAccess() !== true) {

throw new WebServiceException(WebServiceErrorCode::$ACCESSDENIED, 'Permission to write is denied');



//Get related record Id

list($moduleRelatedId, $elementRelatedId) = vtws_getIdComponents($relatedRecordId);


//Get instance for record id

$relatedRecordInstance = Vtiger_Record_Model::getInstanceById($elementRelatedId);

$relatedModuleName = $relatedRecordInstance->getModuleName();


//Get instance for source module

$sourceModuleFocus = CRMEntity::getInstance($sourceModuleName);


if ($sourceModuleFocus && $relatedRecordInstance) {

//Check modules (source / related) and apply remove link

if ($sourceModuleName == 'Potentials' && $relatedModuleName == 'Products') {

$query = 'DELETE FROM vtiger_seproductsrel WHERE crmid=? AND productid=? AND setype=?';

$adb->pquery($query, [$elementSourceId, $elementRelatedId, 'Potentials']);

} elseif ($sourceModuleName == 'Potentials' && $relatedModuleName == 'Contacts') {

$query = 'DELETE FROM vtiger_contpotentialrel WHERE potentialid=? AND contactid=?';

$adb->pquery($query, [$elementSourceId, $elementRelatedId]);

} else {

//Default method to remove link

$sourceModuleFocus->delete_related_module($sourceModuleName, $elementSourceId, $relatedModuleName, $elementRelatedId);




//Buffer save



return true;



Popular Posts

My most popular posts

Maximum productivity on remote job

Maximum productivity on remote job

I started my own business and intentionally did my best to work from anywhere in the world. Sometimes I sit with my office with a large 27-inch monitor in my apartment in Cheboksary. Sometimes I’m in the office or in some cafe in another city.

Hello! I am Sergey Emelyanov and I am hardworker
Business PHP

Hello! I am Sergey Emelyanov and I am hardworker

I am a programmer. I am an entrepreneur in my heart. I started making money from the age of 11, in the harsh 90s, handing over glassware to a local store and exchanging it for sweets. I earned so much that was enough for various snacks.

Hire Professional CRM developer for $25 per hour

I will make time for your project. Knowledge of Vtiger CRM, SuiteCRM, Laravel, and Vue.js. I offer cooperation options that will help you take advantage of external experience, optimize costs and reduce risks. Full transparency of all stages of work and accounting for time costs. Pay only development working hours after accepting the task. Accept PayPal and Payoneer payment systems. How to hire professional developer? Just fill in the form