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?
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
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
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.