HOWTO: After Relationship Add Logic hooks

sugarcrmdevelopers —  November 24, 2010 — 19 Comments

When a record is created from a subpanel you may want to do business logic manipulating the two beans that are going to be linked, but this can’t be done simply with before or after save logic hooks without getting into the $_REQUEST variable. Sugar 6.0 introduced new logic hooks –  after relationship add and after relationship delete –  that addresses this.

For after relationship add, the logic hook is set up like a standard logic hook. In the hook array in custom/moduleName/logic_hooks.php file you add a new entry like the one below.

$hook_array['after_relationship_add'] = Array();
$hook_array['after_relationship_add'][] = Array(1, 'hookName', 'pathToCustomLogicHookClassFile', 'CustomLogicHookClassName', 'CustomLogicHook');

Then in your custom logic hook class file the custom logic hook is defined like below.

function CustomLogicHook(&$bean, $event, $arguments) {

//logic goes here;


Both beans aren’t necessarily loaded, but the id’s and bean names can be accessed via $arguments[‘module’], $arguments[‘related_module’], $arguments[‘id’] and $arguments[‘related_id’] so they can be $bean->retrieve()’ed.

An important thing to remember is that the main module’s custom after_relationship_add logic is called first and then the related module’s custom after_relationship_add logic is called.

This process is similar for after_relationship_delete logic hooks.

19 responses to HOWTO: After Relationship Add Logic hooks


    Why do we need to pass bean by reference? I think that since php 5, objects are always passed by reference.


    Davi you are correct it will automatically be by reference


    Hi, is this logic hook working in communal eidition?


    Yes it is


      I have tried overwriting this in custom bean implementation, but it doesnt fire when relationship is deleted; only when the record is deleted.


    Good Day,

     I am working on and can see (in the .log file) a hook to run  after_relationship_add.  I see this log entry [DEBUG] Hook called: ProspectLists::after_relationship_add

      I have a logic_hook.php file in modules/ProspectLists and it references a file that has a Class in it. 
     $hook_array[‘after_relationship_add’][] = Array($hook_version,’CapHook’,’modules/TB_Cap/cap.php’,’CapHook’, ‘Capstore’);
      As initial means of debug in logic_hook.php file I put a log entry command and do not see it run and the cap.php file has a log entry as well — for example $GLOBALS[‘log’]->debug(‘ [TB] – hook test’);

     I see the log entry  [DEBUG] Hook called: ProspectLists::after_relationship_add  but do not see the output from logic_hook.php nor cap.php which seems to me they are not getting run.

     Can you provide any ideas on where next to debug?

     Thanks for your time


    How can I use a functional of  “after relationship add, after relationship delete” on version 5.1 ?


    Very nice logic hooks. I love the way that the add/delete action triggers both after_relationship_add hooks to be called. I assume this will only work with ‘link’ relationship types? Will it work with Meetings where there is a parent_type and parent_id?


    will it work for Ajax-enabled modules too?

    (Especially after_relation_add -> after_ui_frame loop)


      Yes, it doesn’t care how the relationship is added or removed, so long as it all goes through the core SugarCRM routes.

Trackbacks and Pingbacks:

  1. rencontre en ligne - November 7, 2011

    rencontre en ligne…

    […]SugarCRM Developer Blog » Blog Archive » HOWTO: After Relationship Add Logic hooks[…]…

  2. Handle Condition in Process Record - March 14, 2012

    […] hi eggsurplus, Thanks for your reply. I was not knowing about this logic hook, So i read developers post . but i didnt get it. Will you elaborate how this exactly works for the cases like I […]

  3. New Logic Hooks after_relationship_add and after_relationship_delete - July 12, 2012

    […] HOWTO: After Relationship Add Logic hooks. […]

  4. Subpanel logic hook - December 20, 2012

    […] […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s