AnsweredAssumed Answered

How to grab a filtered list of related records to the current record in a custom module?

Question asked by aqua.alex aqua.alex on Jun 24, 2014
Latest reply on Jun 24, 2014 by aqua.alex aqua.alex
Again I have a very simple problem that should be easy to fix in theory but just not working for me. This problem appeared after updating from 6.0.x CE to 6.5.16 CE

In a special subpage of my module "ICC_TechGroups" all contacts that are related to this group are being loaded.

Old code looked like this:
$beanICC_TechGroups = new ICC_TechGroups();
$beanICC_TechGroups->retrieve($_REQUEST['record']); // loads TechGroup ID
$beanICC_TechGroups->load_relationship('icc_techgroups_contacts');
$contacts = $beanICC_TechGroups->icc_techgroups_contacts->get();
Details to the relation (in database table "relationships"):
relationship_name: icc_techgroups_contacts
lhs_module: ICC_TechGroups
lhs_table: icc_techgroups
lhs_key: id
rhs_module: Contacts
lhs_table: contacts
lhs_key: id
join_table: icc_techgroups_contacts
join_key_lhs: techgroup_id
join_key_rhs: contact_id
relationship_type: many-to-many

In the old version contacts stored all ids of related contacts after get() was executed. In 6.5 it just stored the current "techgroup_id". To fix this I tried using the different examples (http://developer.sugarcrm.com/2012/04/26/howto-grab-a-filtered-list-of-related-record-to-the-current-record-using-beans/ or http://developer.sugarcrm.com/2012/03/23/howto-using-the-bean-instead-of-sql-all-the-time/ or http://support.sugarcrm.com/02_Documentation/04_Sugar_Developer/Sugar_Developer_Guide_6.5/02_Application_Framework/SugarBean/01_Fetching_Relationships/ ), but the results were either an empty Array or even an empty page.

An example would be when I tried to replace the last line ($contacts = ...) with
$contacts = array();
foreach ($beanICC_TechGroups->icc_techgroups_contacts->getBeans(new Contact()) as $contact) {
    $contacts[$contact->id] = $contact;
}
Does anyone have an idea on what the problem might be?

Outcomes