Get data for many-many via beans API

Hi, I am trying to get security group assigned to lead via bean API. But its not working. I tried get_linked_beans as well. Its many-many relationship, so can anyone please point me to how to retrieve ?

    $leadList = $leadBean->get_full_list(
                                    'name',
                                    "    date(leads_cstm.followup_date_c) = date(now())  "
                                    );

  foreach( ($leadList ? $leadList : array())   as $lead ) {
       echo '<br>Processing '.$lead->name; 
       echo '<br> '.$lead->followup_date_c. $lead->campaign_name; 
       
       $lead->load_relationship('securitygroups_leads');
       print_r($lead->SecurityGroups);
     $groups = $lead->SecurityGroups->getBeans();
       
                               

I have a working example of adding security groups that goes like this


            $contact->load_relationship('SecurityGroups');
            $contact->SecurityGroups->add($securityTeam->id);

I didn’t try for retrieving, or for Leads; but I’m sure that it must be the same relationship name (“SecurityGroups”) and if you use that it should work for you.

1 Like

Hi,
Check if it helps you.

https://docs.suitecrm.com/developer/working-with-beans/

Are you able to get LEADS within the loop, i mean are you able to see the output for these lines

   echo '<br>Processing '.$lead->name; 
   echo '<br> '.$lead->followup_date_c. $lead->campaign_name;

Thanks ! you are right. I was using wrong relationship name [securitygroups_leads]. simply using SecurityGroups worked welll.



 $leadBean = BeanFactory::getBean('Leads');
    $leadList = $leadBean->get_full_list(
                                    'name',
                                    "    date(leads_cstm.followup_date_c) = date(now())  "
                                    );

  foreach( ($leadList ? $leadList : array())   as $lead ) {
       $reminder =  '*Reminder*: '.$lead->name;

       $lead->load_relationship('SecurityGroups');
       $groupList = $lead->SecurityGroups->getBeans();

         foreach( ($groupList ? $groupList : array())   as $group ) {
              echo '<br>'.$reminder;
              sendToSlack($group->slack_channel_c,$reminder);
         }
    }