挂钩在SugarCRM自定义字段中添加值

时间:2013-02-26 05:19:30

标签: php sugarcrm

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

这是我的代码:

$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”已停止工作。有没有比这个长查询更简单的方法?

提前致谢。

1 个答案:

答案 0 :(得分:3)

这是做上述事情的更好方法......

自定义/模块/会议/ 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');

自定义/模块/会议/ 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。

修改

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

自定义/扩展/模块/会议/外部/ 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&gt;会议&gt;布局&gt; ListView并将新字段从“隐藏”拖放到“默认”。选择'保存&amp; “部署”按钮,保存会议记录后,您的帐户名称将显示在列表视图中。