php - 在 SugarCRM 自定义字段中添加值的 Hook

标签 php sugarcrm

我是 SugarCRM 的新手。我在 session 模块中创建了一个名为“帐户名称”的自定义字段,这样如果我们从相关字段中选择联系人,该联系人的“帐户名称”就会自动添加到该字段中。

这是我的代码:

$hook_array['after_retrieve'] = Array(); 
$hook_array['after_retrieve'][] = Array(1, 'Add custom account', 
    'custom/modules/Meetings/AddAccount.php','AddAccount', 'addAcc');

LogicHook:

class AddAccount
{
    public function addAcc(&$bean, $event, $arguments)
    {
        global $current_user;
        global $db;

        echo "<pre>";
        $meeting_id = $_REQUEST['record'];
        $query = "SELECT *  FROM `meetings_contacts` WHERE `meeting_id` LIKE '$meeting_id'";
        $result = $bean->db->query($query, true, " Error filling in additional detail fields: ");

        if ($bean->db->getRowCount($result) > 0) {
            while ($row = $bean->db->fetchByAssoc($result)) {
                $contact_id = $row['contact_id'];
            }
            if (isset($contact_id)) {
                $query1 = "SELECT *  FROM `accounts_contacts` WHERE `contact_id` LIKE '$contact_id'";
                $result1 = $bean->db->query($query1, true, " Error filling in additional detail fields: ");

                while ($row1 = $bean->db->fetchByAssoc($result1)) {
                    $account_id = $row1['account_id'];
                }

                $query2 = "SELECT *  FROM `accounts` WHERE `id` LIKE '$account_id'";
                $result2 = $bean->db->query($query2, true, " Error filling in additional detail fields: ");

                while ($row2 = $bean->db->fetchByAssoc($result2)) {
                    $account_name = $row2['name'];
                }

                $update_custom_account = "UPDATE `meetings_cstm` SET `accountname_c` = '$account_name' WHERE `meetings_cstm`.`id_c` = '$meeting_id';";
                $Change = $bean->db->query($update_custom_account);
            }
        }
    }
}

问题是字段正在添加,但 ListView 中的“i”已停止工作。有没有比这个长查询更简单的方法?

提前致谢。

最佳答案

这是执行上述操作的更好方法。

custom/modules/Meetings/logic_hooks.php

// position, file, function
$hook_array['after_retrieve'] = Array();
$hook_array['after_retrieve'][] = Array(1, 'Add custom account', 'custom/modules/Meetings/AddAccount.php', 'AddAccount', 'getAccountName');
$hook_array['after_save'] = Array();
$hook_array['after_save'][] = Array(1, 'Add custom account', 'custom/modules/Meetings/AddAccount.php', 'AddAccount', 'getAccountName');

自定义/模块/Meetings/AddAccount.php

class AddAccount {

    public function getAccountName(&$bean, $event, $arguments) {

       if ($bean->parent_type == 'Contacts') {
            $contact = BeanFactory::getBean('Contacts', $bean->parent_id);
            $contact->load_relationship('accounts_contacts');
            
            $account = BeanFactory::getBean('Accounts', $contact->account_id);
            $bean->account_name_c = $account->name;
        }
    }
}

这样,您将使用 bean 而不是 SQL。

编辑:

要添加新字段,您可以创建此文件...

自定义/扩展/模块/ session /Ext/Vardefs/account_name_c.php

<?php
    $dictionary['Meeting']['fields']['account_name_c'] =
        array (
            'name' => 'account_name_c',
            'vname' => 'LBL_ACCOUNT_NAME_C',
            'type' => 'varchar',
            'len' => '255',
            'unified_search' => true,
            'comment' => 'Account name for meeting',
            'studio' => 'true',
        );

然后,修复/重建后,转到 Studio > session > 布局 > ListView,并将新字段从“隐藏”拖/放到“默认”。选择“保存并部署”按钮,保存 session 记录后,您的帐户名称将显示在 ListView 中。

关于php - 在 SugarCRM 自定义字段中添加值的 Hook ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15081906/

相关文章:

sugarcrm - 订购 SuiteCRM 日历的 session 字段

php - 迁移旧表数据到新表查询

php - 创建可点击的按钮和弹出窗口

google-analytics - 在 SugarCRM 中获取 google 分析报告

javascript - 条件必填字段

php - 如何在 SugarCRM CE 中创建线索和自定义模块之间的关系?

PHP:SQL 查询数组的最佳方法是什么? (如果你可以的话)

php - 无法从mysql检索数据并显示

MediaWiki 安装上的 PHP 和 MySQL

SugarCRM - 在关系表中插入新记录