Example of how to create a after_relationship_delete Logic Hook

Matthew Marum —  January 10, 2013 — 1 Comment

The after_relationship_* logic hooks tend to be some of the more mysterious logic hooks to get going, but are also very useful in that your can trigger all sorts of additional logic to occur once the relationship is added or removed.

Here’s an interesting request that uses one of these hooks…

I have some custom modules,
Module 1 has a one to many relationship with Module 2

When using Module 1, from the subpanel, when I remove a record from Module 2, It just removes the relationship…..Module 2 the record remains just missing the relationship.
However, if I delete the record from Module 2, It is completely removed from Module 1 as well.

I want to write a logic hook, so that when I delete the records from the subpanel in Module 1, it is completely deleted in Module 2.

This is a classic case for the after_relationship_delete logic hook, and you can see is quite easy to do from looking at the below code example…

The key here is to look at the passed arguments, in particular the ‘related_module’ and ‘related_id’ settings, which point right to the record which was before a part of the relationship. All that needs done is to load the record and call the mark_deleted() call ( which is important to do versus just setting deleted to ‘1’ as the mark_deleted() call will do a bunch of other housekeeping bits and also call the *_delete logic hooks ).

Matthew Marum


Matt is the Director of Developer Advocacy for SugarCRM. Previously he was an Engineer on Sugar 7 and a Solutions Architect for the OEM program. He is also an avid trail runner, Boston Marathon qualifier and a karaoke aficionado.

One response to Example of how to create a after_relationship_delete Logic Hook


    Does this accomplish the same thing as replacing the Remove button in the Subpanel with a delete button?

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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