many relationship lead conversion

Hi All,

I have a one to may relationship between leads/contacts and a custom module, so i have a sub panel showing on both leads and contacts, when i convert the lead, the relationship information isn’t converted, I have tried the following code and I hit a white page nothing seems to happen, in the log i can see the relationship load but thats all that happens with it,

I got the PDL_groan and pdl_groan from the Table and Object name in the module folder.


<?php

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('pdl_groan_leads');
   
   //Remove relationship 
   $pdl_groan = array();
   foreach ($bean->pdl_paydayloan->getBeans() as $PDL_groan)
   {
    $PDL_groan_id = $PDL_groan->id;
    $pdl_groan[] = $PDL_groan_id;   
   
    $bean->pdl_groan->delete($lead_id, $PDL_groan_id);
   }
   
   $bean->save();
   
   //Transfer relationship to Contact record
   $contact = new Contact();
   $contact->retrieve($contact_id);
   
   $contact->load_relationship('pdl_groan_contacts');
   foreach ($pdl_groan as $PDL_groan)
   {
    $contact->pdl_groan->add($PDL_groan);
   }
  }
 }
}
?>

Would someone please be able to help with this. thanks

I don’t know the specifics of this, but some generic PHP advice…

  1. Are you working with a debugger? Can you step through the code and see where it fails?

  2. Do you get anything in php_errors.log? If you have a white screen, you likely have a PHP fatal error, and you should have a message there.

  3. You can add logging code in your routine to see if it is getting called.

  4. Where is this file, and how is it loaded by SuiteCRM?

Hi pgr

Thanks for your prompt response

Other than the built in log, no.

Haven’'t checked, didn’t realise this was a php error, which since you’ve said that leads me to then look at the tailing

?>

I will look at this tonight when the system isn’t being used.

What would you suggest to add and where, treat me like a noob, lol

$hook_array['before_save'][] = Array(1, 'DataTransfer', 'custom/modules/Leads/DataTransfer.php','DataTransfer', 'doDataTransfer');

so then the code in post #1 is in DataTransfer.php

Thanks
Ryan

You can learn about logging here

https://suitecrm.com/wiki/index.php/Developer_Guide#Logging

You can also try to set up XDEBUG and an IDE, so all of these things become more transparent to you… that would surely help you find the problem with your logic hook (from looking at it, I can’t find any, although I am not sure about whether some of the things you tried work or not).

hey, i’ve still not resolved this and doing some digging, your are right about the php error, but this seems to be what i am getting,

PHP Fatal error:  Call to a member function getBeans() on a non-object in /var/www/html/admin/custom/modules/Leads/DataTransfer.php on line 18

i can only assume it is something to do with $PDL_Groan in this line

foreach ($bean->pdl_groan->getBeans() as $PDL_groan)

I have resolved the first half of the code where it removes it from the lead not sure what i did, tried renaming the relationship and beans and must have gotten the right comination, but then the second half doesnt work, i now get the same error on line 34.

now I have

<?php

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('pdl_groan_leads');
   
   //Remove relationship 
   $pdl_groan = array();
   foreach ($bean->pdl_groan_leads->getBeans() as $PDL_groan)
   {
    $PDL_groan_id = $PDL_groan->id;
    $pdl_groan[] = $PDL_groan_id;   
   
    $bean->pdl_groan->delete($lead_id, $PDL_groan_id);
   }
   
   $bean->save();
   
   //Transfer relationship to Contact record
   $contact = new Contact();
   $contact->retrieve($contact_id);
   
   $contact->load_relationship('pdl_groan_contacts');
   foreach ($pdl_groan as $PDL_groan)
   {
    $contact->pdl_groan->add($PDL_groan);
   }
  }
 }
}
?>

can’t edit last one, pasted wrong current code

<?php

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('pdl_groan_leads');
   
   //Remove relationship 
   $pdl_groan = array();
   foreach ($bean->pdl_groan_leads->getBeans() as $PDL_groan)
   {
    $PDL_groan_id = $PDL_groan->id;
    $pdl_groan[] = $PDL_groan_id;   
   
    $bean->pdl_groan_leads->delete($lead_id, $PDL_groan_id);
   }
   
   $bean->save();
   
   //Transfer relationship to Contact record
   $contact = new Contact();
   $contact->retrieve($contact_id);
   
   $contact->load_relationship('pdl_groan_contacts');
   foreach ($pdl_groan as $PDL_groan)
   {
    $contact->pdl_groan->add($PDL_groan);
   }
  }
 }
}
?>

If you have an error on line 34 it would help to know exactly what the error text says, and exactly which line in that listing is line 34…

PHP Fatal error:  Call to a member function getBeans() on a non-object in /var/www/html/admin/custom/modules/Leads/DataTransfer.php on line 34
    $contact->pdl_groan->add($PDL_groan);

i did mention i am a n00b didnt i?

pdlgroan is not a member of $contact, so PHP doesn’t know what you mean with $contact->pdlgroan

pdlgroan is a variable in your function (an array, in this case)

$contact->pdl_groan->add($PDL_groan);

so should this be the relationship like

$contact->pdl_groan_contacts->add($PDL_groan);

I think so, yes, but don’t ask me - try it! :slight_smile:

Thanks PGR, i will be doing, however i can’t do anything untill 20:30hrs, people are using the system right now, i seem a solution to do it via workflow, although with that i can’t work out how to trigger the flow on lead convert go figure.

I never tried it, but I guess the “convert” saves both the Lead and the Contact, so you should be able to trigger a “save” workflow.

Closed!

i sorted it, this instance or crm was missing the Converted option from the Status dropdown, therefore it wasn’t getting changed, comparing it with another instance i have, which i seemingly have never had any issues with, I added the converted option back in, now when the lead converts that field is changed, and now the workflow runs,

as described in here:
https://suitecrm.com/suitecrm/forum/suitecrm-7-0-discussion/7849-converting-lead-including-history-attachments

Thanks for your help again though pgr,

you the only admin around here? lol