function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Raja JammulaRaja Jammula 

convert lead in to multiple opportunities

Hi i am working on lead conversion.I need to override the standard 'Convert' button given by salesforce with VisualForce page and write apex code for it. I need help in the following
1. There are many fields(Standard/Custom) in lead that need to be mapped to Account,Contact and Opportunity custom fields. 
2. I have a multi select picklist field 'product' on Lead screen which has values x,y,z
If two products (or three) is selected on Lead Screen then 2 Opportunity records should be created one with product as 'X' and another as 'Y'.
 
Please help me achieve this functionality.Thanks in Advance.
Nithesh NNithesh N
1) You can use Database.LeadConvert Class to get mapping functionlity which you defined declaratively.
     Refer the  following code which may give you some idea. 
lead = [SELECT Id ,LastName,Company,Status, OwnerId FROM Lead WHERE Id =:Lid LIMIT 1];
        Database.LeadConvert ldconvert = new Database.LeadConvert(); 
        ldconvert.setLeadId(lead.id);
        ldconvert.setOwnerId(lead.OwnerId);
        ldconvert.ConvertedStatus = 'Closed - Converted';
        ldconvert.setSendNotificationEmail(false);
        
        try{
            
            Database.LeadConvertResult result = Database.convertLead(ldconvert); 
            System.Debug('Conversion Result: ' + result.isSuccess());
            if(result.isSuccess()){
             isConverted = true;
             lead = [SELECT Id, Name, product__c, ConvertedOpportunityId FROM Lead WHERE Id =:Lid LIMIT 1];
                Opportunity opp1 = [SELECT Id, AccountId, Name FROM Opportunity WHERE Id =:lead.ConvertedOpportunityId LIMIT 1];
                List<String> prods = new List<String>();
                
                prods.addAll(lead.product__c.split(';'));
                List<Opportunity> clonedOpps = CloneOpp(prods.size(), opp1);                
                for(Integer i=0; i < clonedOpps.size(); i++){
                    clonedOpps.get(i).Name = prods.get(i);
                }
                
                Database.Insert(clonedOpps, false);
                delete opp1;                
            }
            
        }
       
        catch(DmlException e){
            
            System.debug(e.getCause()); 
        }
 
public List<Opportunity> CloneOpp(Integer num, Opportunity opp){
    List<Opportunity> opps = new List<Opportunity>();
    for(Integer i = 0 ; i < num ; i++){
        opps.add(opp.clone(false,false,false,false));
     }
      return opps;
    }

As for the visualforce help, You can refer my github repository which has code similar to what you need. 

Let me know if it works.

Best,
Nithesh