如何在SuitCRM/SugarCRM中通过Account关联字段自动填充Contact模块信息
SugarCRM Contact模块自动填充Account字段解决方案
问题说明
我在Contact模块中添加了Account的关联字段,希望从列表选择某一Account后,Contact模块的姓名、邮箱及电话号码字段能自动填充该Account对应的姓名、邮箱和电话。之前在custom/module/contacts/populated fields.php目录下尝试了一段逻辑钩子代码,但完全无效:
<?php if (!defined('sugarEntry') || !sugarEntry) { die('Not a valid entry point.'); } class PopulateFields { function populate($bean, $event, $arguments) { // Check if the Account field has changed if ($bean->account_id != $bean->fetched_row['account_id']) { // Retrieve the selected Account's information $accountBean = BeanFactory::getBean('Accounts', $bean->account_id); // Populate the fields with the Account's information if (!empty($accountBean)) { $bean->name = $accountBean->name; $bean->email1 = $accountBean->email1; $bean->phone_office = $accountBean->phone_mobile; } } } }
有效实现方案
1. 正确的代码文件路径
SugarCRM自定义逻辑钩子的正确存放目录是custom/modules/Contacts/,文件名建议用PopulateFromAccount.php(注意目录名是modules复数,且文件名不要包含空格)。
2. 修正后的逻辑钩子代码
<?php if (!defined('sugarEntry') || !sugarEntry) { die('Not a valid entry point.'); } class PopulateFromAccount { public function populateContactFields($bean, $event, $arguments) { // 仅在创建/编辑页面且account_id有变化时执行 if (empty($bean->fetched_row['account_id']) || $bean->account_id != $bean->fetched_row['account_id']) { if (!empty($bean->account_id)) { $accountBean = BeanFactory::getBean('Accounts', $bean->account_id); if ($accountBean && $accountBean->id) { // 填充Contact对应字段,可根据实际字段名调整 $bean->name = $accountBean->name; $bean->email1 = $accountBean->email1; $bean->phone_mobile = $accountBean->phone_mobile; } } } } }
3. 配置逻辑钩子触发规则
在custom/modules/Contacts/Ext/Hooks/目录下创建populate_from_account.php文件,内容如下:
<?php $hook_array['before_save'][] = Array( 1, '从选中的Account填充Contact字段', 'custom/modules/Contacts/PopulateFromAccount.php', 'PopulateFromAccount', 'populateContactFields' );
4. 使配置生效的步骤
- 保存上述两个文件后,登录SugarCRM后台,进入Admin > Repair > Quick Repair and Rebuild
- 执行修复操作后,清除浏览器缓存,即可测试自动填充功能
原代码无效的原因
- 路径错误:原目录
custom/module/contacts应为custom/modules/Contacts(modules是复数,目录名首字母需大写) - 文件名问题:
populated fields.php包含空格,导致系统无法正确加载文件 - 未注册钩子:仅编写了钩子类,但未在
Ext/Hooks目录中配置触发规则,系统无法识别并执行该逻辑 - 字段对应可能有误:原代码中
$bean->phone_office = $accountBean->phone_mobile,需确认Contact模块的目标电话字段实际名称是否为phone_office
内容的提问来源于stack exchange,提问作者Smith
相关产品推荐
相关产品推荐

