get_linked_beans in after retrive logik hook

Hi,

If I call get_linked_beans method in the after retrive logik hook with N linked beans, it is called 1 + N times (N = amount of related records). For example:


function afterRetrieve($bean, $args, $events)
{

    $xBean = $bean;

    $yBeans = $xBean->get_linked_beans(
        'x_bean_y_bean',
        '',
        array(),
        0,
        -1,
        0,
        ""
    )

    $GLOBALS['log']->fatal("I Was Called");

}

If I have 2 linked records in yBeen, in logfile it will be 3 records ā€œI Was Calledā€.

So, my question is: How i can get linked beans with only one call from after retrive logic hook, may be it better to use SQL?

(without get_linked_beans or if linked beans = 0 in logfile only one record ā€œI Was Calledā€ )

Thanks.

I ma not sure, but maybe something from this article might be of help:

http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_6.5/Module_Framework/Logic_Hooks/Examples/Preventing_Infinite_Loops_with_Logic_Hooks/

If you can get some condition to distinguish the original invocation of the hook from the othersā€¦

1 Like

Thank you, but this describes problem with saving. I am not saving. To test I did next steps.

  1. Clean Install CRM
  2. With module builder create new package
  3. With module builder create new module1
  4. With module builder create new module2
  5. With module builder create relationship one-to-many (module1 -> module2)
  6. Create After Retrieve Hook on module 1
  7. Call get_linked_beans(ā€˜module2ā€™)
  8. There is 3 calls After Retrieve Hook

New details. No matter how many records in a related entity when I call get_linked_beans, after retrieve hook called 3 times instead of 1. It looks like a bug.

Yes, it might be a bugā€¦

You can try using debug_backtrace or debug_print_backtrace in each of those calls into the hook, to get a view of what the call stack looks like in each of those calls. I am not sure, but this might give you some clues as to which parts of the code are calling into the hook.

1 Like

Second call is in the fill_in_relationshipfieds() from related records

So, it seems that the hook is called once for the record, and then once for each ā€œrelateā€ field in the record. Maybe itā€™s not a bug, it seems to be by design.

The question is: when you are inside the hook, can you distinguish between these calls? Can you find some parameter that you can test, to know which case it is?

1 Like

I found sulution with static parameter in the class. And can use prevent duplication of logic in hook.

https://www.sugaroutfitters.com/blog/prevent-a-logic-hook-from-running-multiple-times

But, i cant prevent calling the retrieve() method (it do query to db, so it affects performance).

1 Like