Programmatically Find the Name of the Relationship between two Modules

Matthew Marum —  May 29, 2013 — 3 Comments

Editor’s Note: This post came from Sugar Community member Francesca Shiekh. Thanks Francesca!

I often find myself referencing Jeff Bickart’s “HOWTO: Using the bean instead of SQL all the time.” ( )

But when it comes to using the tips in “Adding and removing related records” finding the right relationship name is always a challenge (it is NOT the relationship_name field in the relationship table!).

Also, I sometimes need to find that relationship name programmatically based on module names.

To address this problem I created the following function which, given two module names, returns the name of the relationship needed to load, add and delete relationships between records. If there is no relationship between the modules the function returns FALSE.

Matthew Marum


Matt is the Lead Developer Advocate for SugarCRM Engineering. 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.

3 responses to Programmatically Find the Name of the Relationship between two Modules


    Thanks John


    I could not get this to work, copy and pasted it in a logic hook and did the following:
    $rel_name = getRelationshipByModules(‘Contacts’, ‘Accounts’);

    Saqib Muhammad April 27, 2016 at 6:02 am

    The above code does not handle all scenarios correctly. Lets suppose, We need to add 2 custom relationships of contacts and calls. Once we run this code, it will only return 1 link.

    The reason is:

    retrieve_by_sides function only handling 1 row result. We need to loop the query result to get all the links.


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