AnsweredAssumed Answered

Convert Lead activities

Question asked by Ivan Khong on Mar 18, 2015
Latest reply on Sep 23, 2016 by Ishani Lad
Hi, I am trying to convert lead activites such as calls, notes and tasks to contacts, Account and opportunity. I am using a logic hook and only be able to convert and transfer leads activities to Contact and Account. Opportunity is converted but its activities is empty. Below is the logic hook code:

<?phpif (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

class LeadsTransfer{

  function transfer($bean, $event, $arguments) {
    $activities = array('calls', 'emails', 'meetings', 'notes', 'tasks');
    $relatedModules = array('Contact','Opportunity','Account');
    $related_id = array('contact_id','opportunity_id','account_id');

    // Only execute on Lead Conversion
    if($bean->converted != $bean->fetch_row['converted'] && $bean->converted == 1) {
      $data = array();

      // Get IDs of all related activities
      foreach($activities as $activity) {
        $bean->load_relationship($activity);

        $data[$activity] = array();
        foreach($bean->$activity->getBeans() as $a) {
          $data[$activity][] = $a->id;
        }
      }

      // Perform for Accounts and/or Contacts
      foreach($relatedModules as $k=>$module) {

          $rel = new $module();
          $rel->retrieve($bean->$related_id[$k]);
   //file_put_contents("test.txt","HELLO",FILE_APPEND | LOCK_EX);
          foreach($data as $activity=>$ids) {
            $rel->load_relationship($activity);

            // Add each activity to the related record
            foreach($ids as $id) {
              $rel->$activity->add($id);
            }
          }

          $rel->save();

      }

      $bean->save();
    }
  }
}

Outcomes