AnsweredAssumed Answered

Using Beans to Associate Records in New Relationship Account with Lead

Question asked by Jeff C on Apr 14, 2016
Latest reply on Apr 14, 2016 by kbrill

I created a new many to many relationship in the Lead module associated with the Account module so I can associated Accounts with Leads and vice versa.  This code worked for associating contacts with accounts but I can't get it to associate an account with a lead.  This is just a command line utility I wrote to search for Accounts that have matching info with Lead records and associate the account with that lead.  Any ideas what I'm doing wrong?

 

(this is the code inside the loop that reads the lead records into $lead_row)

 

        $result_account = $db->query("SELECT id FROM accounts WHERE (name LIKE '{$lead_row['account_name']}') or (billing_address_street LIKE '{$lead_row['primary_address_street']}') or (phone_office LIKE '{$lead_row['phone_work']}') or (phone_office LIKE '{$lead_row['phone_other']}')");

 

  while ($account_row = $db->fetchByAssoc($result_account))

                {

                echo "matched {$lead_row['account_name']}\n";

                $bean = new Lead();

                $bean->retrieve($lead_row['id']);

 

                $bean->load_relationship('accounts');

                // Uncomment to write changes

                $bean->accounts->add($account_row['id']);

                $bean->save();

                }

Outcomes