HOWTO: Using get_linked_beans() method to grab related records

sugarcrmdevelopers —  February 23, 2011 — 21 Comments

One of the strengths of SugarCRM is that any record can be very easily related to another one. The platform supports one-to-one, one-to-many, and many-to-many relationships, giving you plenty of flexibility is designing your application.

In this blog post, we’ll see how thru code we can get a list of all of the related records to a particular record. This example will deal with grabbing related contacts to an account. Let’s take a look at the code

They key method here is get_linked_beans(), which takes two default arguments. The first is the name of the link field that represents the relationship; you can find this info out in the vardefs.php file for the module. The second argument is the name of the bean you’ll be creating for each found related record. So in this instance, we’ll be looking over the ‘contacts’ link field for the relationship to use, and for each found record we’ll create a Contact bean record. The return value is an array of beans ( in this case, Contact beans ). We iterate over the beans and output the name of the contact.

21 responses to HOWTO: Using get_linked_beans() method to grab related records


    Great, thank you!
    In which file would I include this code?


    You can use it anywhere in the app you would need to add this functionality.


    This is exactly what I need


    John, btw if you include this article in your book “The Definitive guide to SugarCRM”, it would be awesome.


    Unfortunately while there is a third parameter sort_fied (array) in get_linked_beans, it is ignored in the generated query


    I wonder why do you need all these parameters except first?


    Does this work custom modules and custom relationships. For example I have a Bookings module and a Session Module with a 1 to many relationship. I’m looking how I can just show all the sessions for a particular booking in the list view. Where would the above code go?

    Many thanks in advance (Sugar Noooob)


    Yes, it usefull. But how I can put order_by criteria for related list?
    Before version 6.3.1 it could be done using get_related_list of SugarBean method, but starting from 6.3.1 this method is broken.


    its not working at all.
    Lots off error messages. I put it in the display funtion of the view.edit.php


       What are the error messages?


        ( ! ) Parse error: syntax error, unexpected T_OBJECT_OPERATOR in C:wampwwwSugarCrmmodulesAccountsviewsview.edit.php on line 38Call Stack#TimeMemoryFunctionLocation10.0004372608{main}( )..index.php:020.274215170408SugarApplication->execute( )..index.php:3630.352817999440SugarController->execute( )..SugarApplication.php:8140.359018353384SugarController->processView( )..SugarController.php:28050.359018353440ViewFactory->loadView( )..SugarController.php:29460.371919380024ViewFactory->_buildFromFile( )..ViewFactory.php:69


           Which line of code is triggering this? And can you show the code you are using?


            important question, will it be possible with this script to populate fields on my form with data linked to the selected field. I mean just after selection of a related field ? In fact there has no save been done yet. Something like a afterselection event.


    I have a related field. When I have selected a value from the related field,  I want to fill some boxes. Is there an option for this except the related function ?


    How to retrive records which are associated by relate field not by any relationship ?
    Thanks for help:)

Trackbacks and Pingbacks:

  1. Trouble using get_linked_beans() for Notes - SugarCRM Forums - November 2, 2011

    […] […]

  2. Best way to get field values from other modules? - June 19, 2012

    […] […]

  3. How to count the subpanel records - November 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