Programmatically Find the Name of the Relationship between two Modules

Sugar Dev Team —  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.” (http://developers.sugarcrm.com/wordpress/2012/03/23/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.

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

  1. 

    Nice
    Thanks John

  2. 

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

  3. 
    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.

    Thanks

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