AnsweredAssumed Answered

$bean->retrieve($contact_id) fills memory (with one contact)

Question asked by Ramblin Ramblin on Feb 17, 2014
Latest reply on Feb 17, 2014 by Ramblin Ramblin
I am trying to do something I have done before but this time it is apparently cycling through and filling up the memory so it error out with a memory overflow.

Note that this is being done on a fresh install with only one Contact in the system (as a test Contact) so it is not as if the system truly is doing a lot of work and needs a lot of memory.

I want to create a vardef called account_type_imported in Contacts to pull in the Acccount Type from the Accounts module.

I do NOT want to make it a saved field in the dbase since the Account Type may change and I want the user looking at teh Contacts views to always get the current value, so I create it as a non-dbase vardef and pull the value in with an after-retrieve logic hook every time the user views the Contact.  (I do NOT want to use a relate field since I also pull the value in with a process_record logic hook so it can be used in subpanels, but if I can get the first one working, I can do the second one, so for this question, I'll just focus on the after_retrieve hook operation).

All seems to work as it has for me before, up until the logic hook is activated and it tries to retrieve the Contact record using getBeans() and then it errors out with a memory overflow.

Can anyone see what I am doing wrong?

Here is the setup

In custom/Extension/modules/Contacts/Ext/Vardefs/sugarfield_account_type_imported.php
<?php

$dictionary['Contact']['fields']['account_type_imported'] = array
(
      'name' => 'account_type_imported',
      'vname' => 'LBL_ACCOUNT_TYPE_IMPORTED',
      'type' => 'readonly',
      'source' => 'non-db',
      'dbType' => 'non-db',
      'studio' => 'visible',
);

?>

In custom/Extension/modules/Contacts/Ext/Language/en_us.account_type_imported.php
<?php

$mod_strings['LBL_ACCOUNT_TYPE_IMPORTED'] = 'Account Type';

?>

In custom/modules/Contacts/logic_hooks.php (add the after_retrieve)
<?php
// Do not store anything in this file that is not part of the array or the hook version.  This file will
// be automatically rebuilt in the future.
$hook_version = 1;
$hook_array = Array();
// position, file, function
$hook_array['after_retrieve'] = Array();
$hook_array['after_retrieve'][] = Array(1, 'Fetch Account Type for detailview', 'custom/modules/Contacts/populate_account_type.php','fetch_account_type_value','get_data_from_accounts');
$hook_array['before_save'] = Array();
$hook_array['before_save'][] = Array(1, 'Contacts push feed', 'modules/Contacts/SugarFeeds/ContactFeed.php','ContactFeed', 'pushFeed');
$hook_array['after_ui_frame'] = Array();
$hook_array['after_ui_frame'][] = Array(1, 'Contacts InsideView frame', 'modules/Connectors/connectors/sources/ext/rest/insideview/InsideViewLogicHook.php','InsideViewLogicHook', 'showFrame');
?>

In in custom/modules/Contacts/populate_account_type.php
<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

class fetch_account_type_value
{

function get_data_from_accounts($bean, $event, $arguments)
{
  $relationship_name = 'accounts_contacts';
  // Fetch the related Contacts object
  $contact_object_id = $bean->id;
  $contact_object = new Contact();
  $contact_object->retrieve($contact_object_id);
  $contact_object->load_relationship($relationship_name);
  $list_returned = $contact_object->$relationship_name->getBeans();
  // Update the account_type_imported vardef
  if ( !empty($list_returned) )
  {
   reset($list_returned);
   foreach ($list_returned as $record);
   {
    $bean->account_type_imported = $record->account_type;
   }
  }
}
}

?>

Run a repair and rebuild

Try to view a Contact and I get the error message (after a delay):
Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to   allocate 512 bytes) in   /var/www/html/crm/include/database/MysqliManager.php on line 244 


The same code has previously worked between two custom modules (different relationship name ...) but hangs here

Can anyone see what I am doing wrong?


Outcomes