How to create custom field in contact module in vtiger CRM

1.1k views Asked by At

I did this code using the internet ,But it isn't working.I save this file in the root directory.i need to create a mandatory field and dropdown field in contact module.

<?php

$Vtiger_Utils_Log = true;
ini_set('error_reporting', E_ALL);
ini_set('display_errors', '1');
include_once('../../config.inc.php');
set_include_path($root_directory); //for include root path
include_once('vtlib/vtiger/menu.php');
include_once('vtlib/Vtiger/Module.php');
include_once('vtlib/Vtiger/Block.php');



$moduleInstance = Vtiger_Module::getInstance('Contacts');//Module Name
$blockInstance = Vtiger_Block::getInstance('LBL_CONTACT_INFORMATION', $moduleInstance);//Block Name


$fieldInstance = new Vtiger_Field();
$fieldInstance->name = 'whatsapp';
$fieldInstance->label = 'LBL_WHATSAPP';
$fieldInstance->table = 'vtiger_contactdetails';
$fieldInstance->column = 'whatsapp';
$fieldInstance->columntype = 'varchar(11)';
$fieldInstance->uitype = 1;
$fieldInstance->typeofdata = 'V~O';
$blockInstance->addField($fieldInstance);
$fieldInstance->setRelatedModules(array('Accounts'));

?>
2

There are 2 answers

1
Manolo On

your code seems fine, I would recommend you check module and block existance before creating the field. Are you sure your database is connecting fine? The logs folder show no error?

This is how I would make to add a field.

<?php

$Vtiger_Utils_Log = true;
ini_set('error_reporting', E_ALL);
ini_set('display_errors', '1');
include_once('../../config.inc.php');
set_include_path($root_directory); //for include root path
include_once('vtlib/vtiger/menu.php');
include_once('vtlib/Vtiger/Module.php');
include_once('vtlib/Vtiger/Block.php');



$moduleInstance = Vtiger_Module::getInstance('Contacts');//Module Name
if($moduleInstance){
    $blockInstance = Vtiger_Block::getInstance('LBL_CONTACT_INFORMATION', $moduleInstance);//Block Name

    if($blockInstance){
        $fieldInstance = Vtiger_Field::getInstance('whatsapp', $moduleInstance);
        if(!$fieldInstance){
            $fieldInstance = new Vtiger_Field();
            $fieldInstance->name = 'whatsapp';
            $fieldInstance->label = 'LBL_WHATSAPP';
            $fieldInstance->table = 'vtiger_contactdetails';
            $fieldInstance->column = 'whatsapp';
            $fieldInstance->columntype = 'varchar(11)';
            $fieldInstance->uitype = 1;
            $fieldInstance->typeofdata = 'V~O';
            $blockInstance->addField($fieldInstance);
            $fieldInstance->setRelatedModules(array('Accounts'));
        }
        else{
            echo "field already present";
        }
    }
    else{
        echo "no block";
    }
}
else{
    echo "no module";
}

?>
0
kapsule On

If you need online help with the VTiger Open Source platform, I recommend you visit the Telegram forum. https://t.me/vtiger7crm