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



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){

    if(!$related['success']) return $related;

    //opportunity creation
    $opp->name="Some name";
    $date_closed = new DateTime();
    $date_closed->add(new DateInterval('P2M'));
    //TODO : populate opp fields with lead fields (same name + conversion mapping)   

    //convert the lead

    $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 :

    //copy every "same name" lead field to opportunity
    foreach ($lead->field_defs as $field => $def) {
      if($def['source']=='non-db') continue;

    //copy fields defined in the convert mapping
    $metaDataFile = SugarAutoLoader::existingCustomOne("modules/Leads/clients/base/layouts/convert-main/convert-main.php");
    foreach($viewdefs['Leads']['base']['layout']['convert-main']['modules'] as $i=>$module){
    foreach($mappingOpp as $oppField=>$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