2

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

这是我的代码:

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

逻辑钩:

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

提前致谢。

4

1 回答 1

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。

编辑:

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

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

于 2013-02-27T20:19:49.497 回答