phpsugarcrm

Hook to Add a Value in a SugarCRM Custom Field


I am new to SugarCRM. I've created a custom field named 'account name' in the Meetings module so that if we select Contacts from related to field, the 'account name' of that Contact is automatically added to the field.

Here's my code:

$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);
            }
        }
    }
}

The problem is that the field is getting added but the "i" in the ListView has stopped working. Is there a simpler way than this long query?

Thanks in advance.


Solution

  • This is a better way of doing the above.

    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');
    

    custom/modules/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;
            }
        }
    }
    

    This way, you are using the bean and not SQL.

    EDIT:

    To add the new field, you can create this file…

    custom/Extension/modules/Meetings/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',
            );
    

    Then after a Repair/Rebuild, go to Studio > Meetings > Layouts > ListView and drag/drop the new field from 'Hidden' to 'Default.' Select the 'Save & Deploy' button, and after saving the Meeting record, your account name will appear in the list view.