Leads to Leads Relationship on Conversion

Hey guys, so I have a client with Leads who may have ā€œSiblingsā€ who are also leads.

Created leads_leads_1 relationship no problem.

However, on convert to Contact I want to keep that relationship. I think I need to have a corresponding leads_leads_1 relationship. However, can’t create that in Studio on the Contacts side.

Can I just create this relationship programmatically?

What exactly did you try in Studio?

Contact to Contact? A second contact to Lead, apart from the default one?

It should be possible…

Thanks @pgr, I can’t select Leads -Leads in the contact module because the first field always has to be Contacts and it won’t let you change it. I also noticed that I already have a leads_leads_1MetaData.php in custom, so I can’t create another with the same name for contacts. Kind of stumped on how to do it, unless with an after save hook on new accounts maybe? And just populate a contacts_leads relationship and call it ā€œSiblingsā€.

Hopefully I was clear, so here is what I’m trying to do

LEADS (conversion =>) CONTACTS
leads_leads - Relationsip (m2m) => contacts_leads -Relationship (m2m)

I think the new relationship, between the sibling Lead and the newly created Contact, is a Contacts-Lead relationship for sure.

But yes, you’ll need some custom code to make it happen. I’m not sure you can do it from a logic hook, but it’s worth a try.

If not, then perhaps some customization of the conversion code. In the same place where it’s copying fields over from the Lead to the Contact.

Have a look at this PR where I made a similar enhancement:

I’m not sure about doing that in an upgrade-safe way, you’ll also have to investigate that part.

Thanks @pgr I’ll try that. Just got side tracked, but back on this project shortly.

@pgr I’m trying out this: Angel's Blog: Logic Hooks: Lead History Transfer
I tested it with ā€œnotesā€ as in the tutorial and it works great!

However, I’m struggling to get it to work for my case. I know it’s firing because it’s removing the relationship from the lead. Just not creating it on the contact. No errors are generated. Does this look right? Or will it only work with a one-to-many?

     class DataTransfer
 {
  function doDataTransfer($bean, $events, $arguments)
  {
   $action  = $_REQUEST['action'];
   
   if ($action == 'ConvertLead') //Must confirm it only triggers on conversion!!
   {
    $lead_id  = $bean->id;
    $contact_id = $bean->contact_id;
     
    $bean->load_relationship('leads_contacts_1');
    
    //Remove relationship 
    $rcontacts = array();
    foreach ($bean->leads_contacts_1->getBeans() as $rcontact)
    {
     $rcontact_id = $rcontact->id;
     $rcontacts[] = $rcontact_id;   
    
     $bean->leads_contacts_1->delete($lead_id, $rcontact_id);
    }
   
    $bean->save();
    
    //Transfer relationship to Contact record
    $contact = new Contact();
    $contact->retrieve($contact_id);
    
    $contact->load_relationship('leads_contacts_1');
    foreach ($rcontacts as $rcontact)
    {
     $contact->leads_contacts_1->add($rcontact);
    }
   }
  }
 }

Whoo Hoo! Got it working duh, the relationship on the ā€œContactsā€ side should be contacts_contacts_1 not leads_contacts_1

If anyone else needs… here’s the working code:
Here’s the working code: on the lead side the relationship is leads_contacts_1
on the contact side the relationship is contacts_contacts_1

 class DataTransfer
 {
  function doDataTransfer($bean, $events, $arguments)
  {
   $action  = $_REQUEST['action'];
   
   if ($action == 'ConvertLead') //Must confirm it only triggers on conversion!!
   {
    $lead_id  = $bean->id;
    $contact_id = $bean->contact_id;
     
    $bean->load_relationship('leads_contacts_1');
   
    //Remove relationship 
    $rcontacts = array();
    foreach ($bean->leads_contacts_1->getBeans() as $rcontact)
    {
     $rcontact_id = $rcontact->id;
     $rcontacts[] = $rcontact_id;   
    
     $bean->leads_contacts_1->delete($lead_id, $rcontact_id);
    }
   
    
    //Transfer relationship to Contact record
    $contact = new Contact();
    $contact->retrieve($contact_id);
    
    $contact->load_relationship('contacts_contacts_1');
    foreach ($rcontacts as $rcontact)
    {
     $contact->contacts_contacts_1->add($rcontact);
    }
   }
  }
 }

This is setup as a logic hook in custom/modules/Leads/{name of logic hook}. php

1 Like

Quick note: if anyone else uses this method, you can see I removed $bean->save() the second time around. It seems to work OK if you’re only updating one relationship. Once I tried a second logic hook to update another relationship (leads to lead) it created an infinite loop and hung. It works without $bean->save() and doesn’t hang if you try to copy more than one relationship.

Very nice! :tada:

(See the edits I made to your posts to learn about how to get the nice syntax highlighting)

1 Like