AnsweredAssumed Answered

Convert a lead through code on server side

Question asked by Groupe Sugar on Oct 17, 2018
Latest reply on Oct 17, 2018 by Oriol Barbeta

Hello,

 

I'm trying to drive a lead conversion form server side (SugarCRM ENT 8.0.1).

The contact and the account to be created are imported form another system, and the opportunity has to be created form the existing lead. For now I'm able to convert my lead using the following code:

public function importAndConvert($api, $args){

    $related=$secutix->importContactfromExternalSystem($args['externalId']);
    if(!$related['success']) return $related;

    $lead=BeanFactory::retrieveBean('Leads',$args['lead_id']);
   
    //opportunity creation
    $opp=BeanFactory::newBean('Opportunities');
    $opp->name="Some name";
    $date_closed = new DateTime();
    $date_closed->add(new DateInterval('P2M'));
    $opp->date_closed=$date_closed->format('Y-m-d');
    //TODO : populate opp fields with lead fields (same name + conversion mapping)   
    $opp->save();

    //convert the lead
    $modules=array(
      'Accounts'=>BeanFactory::retrieveBean('Accounts',$related['account']),
      'Contacts'=>BeanFactory::retrieveBean('Contacts',$related['contact']),
      'Opportunities'=>$opp,
    );

    $leadConvert=new LeadConvert($lead->id);
    $leadConvert->convertLead($modules, 'move', array('Opportunities'));

    return array('success'=>true);
}

It works fine but I want to populate the opportunity as if the conversion would have been done through the interface, that is to say copy the fields with the same name and take into account the fieldMapping defined in modules\Leads\clients\base\layouts\convert-main\convert-main.php.

I haven't found anything allready built in Sugar (I think it's done on client side). Is there something usefull that can be re-used in that peuprose in Sugar?

I tried the following code :

    $lead=BeanFactory::retrieveBean('Leads',$args['lead_id']);
    $opp=BeanFactory::newBean('Opportunities');
    //copy every "same name" lead field to opportunity
    foreach ($lead->field_defs as $field => $def) {
      if($def['source']=='non-db') continue;
      if(isset($opp->field_defs[$field])){
        $opp->{$field}=$lead->{$field};
      }
    }

    //copy fields defined in the convert mapping
    $metaDataFile = SugarAutoLoader::existingCustomOne("modules/Leads/clients/base/layouts/convert-main/convert-main.php");
    require_once($metaDataFile);
    $mappingOpp=array();
    foreach($viewdefs['Leads']['base']['layout']['convert-main']['modules'] as $i=>$module){
      if($module['module']=='Opportunities'){
        $mappingOpp=$module['fieldMapping'];
        break;
      }
    }
    foreach($mappingOpp as $oppField=>$leadField){
      $opp->{$oppField}=$lead->{$leadField};
    }

 

But when I require $metDataFile I get an error 500 with message "Could not retrieve lead convert metadata."

 

Any idea about what I'm doing wrong, or in which direction I should go?

Matt Marum Francesca ShiekhAngel MaganaCédric Mourizard

 

Thanks,

 

Sylvain

Outcomes