You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vtiger CRM联系人模块自定义字段创建问题:代码无效,需必填及下拉字段

Fixing Field Creation Issues in Vtiger CRM Contacts Module

Let's sort out why your field creation script isn't working, and add the missing dropdown field you need for the Contacts module. Here's what was off in your original code, plus a complete working version:

Key Issues in Your Original Script

  1. Incorrect File Path: Since you're placing the script in the root directory, ../../config.inc.php points to a non-existent location — you just need config.inc.php directly.
  2. Non-Mandatory Field: Your WhatsApp field was set as optional (typeofdata = 'V~O'), but you wanted it to be required.
  3. Missing Dropdown Field Code: There was no logic to create the dropdown field you mentioned.
  4. No Cache Refresh: Vtiger needs a cache reset to recognize new fields.

Complete Working Script

Save this as add_contacts_fields.php in your Vtiger root directory:

<?php
$Vtiger_Utils_Log = true;
ini_set('error_reporting', E_ALL);
ini_set('display_errors', '1');

// Load core config from root directory
include_once('config.inc.php');
set_include_path($root_directory);

// Include required Vtiger libraries
include_once('vtlib/vtiger/menu.php');
include_once('vtlib/Vtiger/Module.php');
include_once('vtlib/Vtiger/Block.php');

// Get Contacts module instance (fail fast if module isn't found)
$moduleInstance = Vtiger_Module::getInstance('Contacts');
if (!$moduleInstance) {
    die("Error: Contacts module could not be loaded!");
}

// Get the main contact information block
$blockInstance = Vtiger_Block::getInstance('LBL_CONTACT_INFORMATION', $moduleInstance);
if (!$blockInstance) {
    die("Error: LBL_CONTACT_INFORMATION block not found in Contacts module!");
}

// --------------------------
// 1. Create Required WhatsApp Field
// --------------------------
$whatsappField = new Vtiger_Field();
$whatsappField->name = 'whatsapp';
$whatsappField->label = 'LBL_WHATSAPP';
$whatsappField->table = 'vtiger_contactdetails';
$whatsappField->column = 'whatsapp';
$whatsappField->columntype = 'varchar(20)'; // Longer for international numbers
$whatsappField->uitype = 1; // Single-line text input
$whatsappField->typeofdata = 'V~M'; // V = Character type, M = Mandatory (required)
$blockInstance->addField($whatsappField);
$whatsappField->setRelatedModules(array('Accounts')); // Link to Accounts if needed

// --------------------------
// 2. Create Dropdown Field (e.g., Contact Type)
// --------------------------
$contactTypeField = new Vtiger_Field();
$contactTypeField->name = 'contact_type';
$contactTypeField->label = 'LBL_CONTACT_TYPE';
$contactTypeField->table = 'vtiger_contactdetails';
$contactTypeField->column = 'contact_type';
$contactTypeField->columntype = 'varchar(100)';
$contactTypeField->uitype = 16; // UIType for dropdown lists
$contactTypeField->typeofdata = 'V~M'; // Set to mandatory (use 'V~O' for optional)

// Add your dropdown options here
$contactTypeField->setPicklistValues(array('Customer', 'Lead', 'Partner', 'Vendor'));

$blockInstance->addField($contactTypeField);

// Refresh module tables and trigger cache update
$moduleInstance->initTables();
echo "Success! Fields created. Please clear Vtiger's cache from the admin panel to see the changes.";
?>

How to Run This

  1. Upload the script to your Vtiger root directory.
  2. Run it either via:
    • Browser: Visit http://your-vtiger-domain/add_contacts_fields.php
    • CLI: SSH into your server and run php add_contacts_fields.php
  3. Check the output for success messages.
  4. Critical Step: Log into your Vtiger admin panel, go to Admin > System Settings > Clear Cache to refresh the system's field registry.
  5. Verify the fields exist by going to Settings > Module Layouts and Fields > Contacts — you can adjust their placement here if needed.

Additional Notes

  • Permissions: Make sure your web server user (e.g., www-data) has write access to the Vtiger directory and database.
  • Field Length: I increased the WhatsApp field length to varchar(20) to support international country codes.
  • Dropdown Options: Modify the setPicklistValues array to match your actual dropdown needs.

内容的提问来源于stack exchange,提问作者Sivabanu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 13:37:36