Moving a Relationship on Convert Lead -> Contact/Opportunity

What I’m trying to do is have a “referer” for the lead which is a contact. When I convert the a lead to a contact, I want the lead the relationship Lead → referrer (one to many leads_contacts) to move over to Contacts → Opportunities (one to Many). In the past I’ve added to a custom view.convertlead.php (around line 431) this:

$lead->load_relationship('contacts_leads_1');
					$coopProspects = $lead->contacts_leads_1->getBeans();
					$coopProspect = reset($coopProspects);


					$beans[$module]->load_relationship('contacts_opportunities_2');
					$beans[$module]->contacts_opportunities_2->add($coopProspect);

and it has worked just fine. And it does (in this case) as long as I don’t create a contact, but instead select one on conversion. If I create one in the conversion process (not the contact the lead is related to, but creating a contact from the lead). It errors out. I’ve been staring at the error message for like 2 days and I just can’t figure out what’s going on. It’s probably so simple, but I’ve been looking at it too long. Anyone see what’s going on here?
Here’s the error I’m getting on convert:

Notice: Undefined property: Account::$contacts_opportunities_2 in /home/crm/public_html/custom/modules/Leads/views/view.convertlead.php on line 442

Fatal error: Uncaught Error: Call to a member function add() on null in /home/crm/public_html/custom/modules/Leads/views/view.convertlead.php:442 Stack trace: #0 /home/crm/public_html/custom/modules/Leads/views/view.convertlead.php(77): ViewConvertLead->handleSave() #1 /home/crm/public_html/include/MVC/View/SugarView.php(210): ViewConvertLead->display() #2 /home/crm/public_html/include/MVC/Controller/SugarController.php(432): SugarView->process() #3 /home/crm/public_html/include/MVC/Controller/SugarController.php(363): SugarController->processView() #4 /home/crm/public_html/include/MVC/SugarApplication.php(101): SugarController->execute() #5 /home/crm/public_html/index.php(52): SugarApplication->execute() #6 {main} thrown in /home/crm/public_html/custom/modules/Leads/views/view.convertlead.php on line 442

I still am pretty new to working with beans, and don’t 100% get it yet, so I’m sure I’m missing something fundamental. Please help!

Note: line 442 is the last line.

@pstevens

The first line of the error contains all the information.
The “Account” object does not have the “contacts_opportunities_2” relationship.
You are writing about object “Opportunity” but really working with object “Account”.

Thanks @p.konetskiy I really appreciate your help! :smiley: that’s exactly what I’m not understanding. contacts_leads_1 is a relationship between leads and contacts and contacts_opportunties_2 is relationship between contacts and opportunities. I’m not understanding how the “Account” comes in to play here?

@pstevens

$beans[$module]

It is an Account object. I don’t know how it’s happens.
I am using usually the code to print data to log .

$GLOBALS['log']->fatal(get_class()." ". __FUNCTION__." <variable name>:\n ".print_r(<variable name>,true));

fatal - the log level.
<variable name> - The name of the variable whose data I want to parse.

Can I force the $module to be Contacts? I’ve put it in the view.convertlead.php where the contacts are created. You’d think the module would be contacts.

@pstevens
The data of variable $module should be equal “Opportunities”, because the variable $coopProspects has object Contact .
I think you should add some check, for en example:

if ($module == 'Opportunities') {
...
}
1 Like

Dude! Thank you so much @p.konetskiy that solved the problem!!! It works great. Still don’t understand why the $module would be Accounts. But putting the if statement in so that it only runs if $module is Opportunities makes it work in all cases. Here’s the code if anyone else needs it, so basically what’s going on is I’m transferring the relationship from lead (contact->lead) to the Opportunity (Contact → Opportunity). I inserted this in view.conertlead.php in /custom/modules/Leads/views (copy it from the module) and then edit about line 431:

 $lead->load_relationship('contacts_leads_1');
					$contactProspects = $lead->contacts_leads_1->getBeans();
					$contactProspect = reset($contactProspects);

				if ($module =='Opportunities'){
					$beans[$module]->load_relationship('contacts_opportunities_2');
					$beans[$module]->contacts_opportunities_2->add($contactProspect);
                }