I am trying to convert a lead and then attach an existing opportunity on it.

Below is my sample piece of code.

Public static void convertLeadUponDealApproval(String LeadRec,Id OpptyId){ 
    Id accountId;
    Id LeadId;
    Id ownerId;
    Lead LeadRecd;
    Opportunity opp;
    list<lead> LstLeadRecd= new list<lead>();
    if(OpptyId != null){
        opp = [Select id, accountId,ownerId from Opportunity where Id =: OpptyId limit 1];
        accountId = opp.accountId;
        ownerId = opp.OwnerId;

    if(LeadRec != null && LeadRec != ''){
       LstLeadRecd = [select id,Lead_ID__c,Custom_ConvertedOpportunityId__c from lead where isConverted= false and Lead_ID__c = : LeadRec limit 1];
       LeadId = LeadRecd.Id;
       LeadRecd.Custom_ConvertedOpportunityId__c = opp.Id;
       LeadRecd.Ignore_Workflow__c = true;
       update LeadRecd;

    Database.LeadConvert lc = new database.LeadConvert();
    //lc.setOpportunityName(OpptyId);  //I am unable to do this.Are there any alternative? 
    lc.setConvertedStatus('4 New Opportunity'); 
    Database.LeadConvertResult lcr = Database.convertLead(lc);
  • 1
    what exact issue you are facing? – Santanu Boral Mar 29 '17 at 18:08
  • I am unable to link an existing opportunity to lead. So that it is visible on the page, which appears after we try to open a converted lead. – Sam R Mar 29 '17 at 18:12

You're absolutely correct. Your only two choices are to create a new opportunity or not. You can't link the converted lead to an existing opportunity in a way that it will appear on the "lead has been converted" screen.

  • Thanks @sfdcfox. But do we have any alternative? – Sam R Mar 29 '17 at 18:40
  • 1
    @SamR No, there really isn't. The ConvertedOpportunityId field is read-only, you can't merge opportunity records, etc. Your only real choice would be to delete the old opportunity and clone the data atop the new opportunity after conversion. It won't be a perfect fit, of course, but I don't see much in the way of alternatives. – sfdcfox Mar 29 '17 at 18:52

This may not be exactly what you are looking for, but I was able to do something similar, and you may be able to adapt it to your needs.

        //update the leads
        //LeadConvert can only handle 100 records at a time
            //Update Opp close date with map of data
            Map<id,id> leadOppMap = new Map<id,id>();
            Map<id,id> leadContMap = new Map<id,id>();
            List<Opportunity> updateOppList = new List<Opportunity>();

            //convert the leads
            for(Integer i = 0; i <= leadConvertList.size()/100 ; i++){
                list<Database.LeadConvert> tempList = new list<Database.LeadConvert>();
                Integer startIndex = i*100;
                Integer endIndex = ((startIndex+100) < leadConvertList.size()) ? startIndex+100: leadConvertList.size();
                for(Integer j=startIndex;j<endIndex;j++){
                //system.debug('tempList conversion = ' + tempList);
                Database.LeadConvertResult[] lcrList = Database.convertLead(tempList, false);
                for(Database.LeadConvertResult lcr : lcrList){
                        //log internal error                            

                        //revert conversion checkboxes
                        Lead revLead = new Lead(id = lcr.getLeadId(), Convert_No_Opp__c = false, Convert_with_Opp__c = false);
                    } else {
                if(revertList.size()>0){update revertList;}

            //after convert, update any opportunities
            for(Lead l: givenLeads){
                if(leadOppMap.containsKey(l.Id) && leadOppMap.get(l.Id)!=null){
                    updateOppList.add(new Opportunity(id = leadOppMap.get(l.Id),
                                                      CloseDate = l.ExpectedStartDate__c,
                                                      name = l.LastName + ' New Enrollement',
                                                      contact__c = leadContMap.get(l.Id),
                                                      type = 'LIS',
                                                      StageName = 'Enrolled'
                                                      //record type

            //update opportunities
                //System.debug('Inserting updateOppList: ' + updateOppList);
                Database.SaveResult[] oppDML = Database.update(updateOppList,false);
                for(Database.SaveResult sr: oppDML){
                    if(sr.isSuccess() && !Test.isRunningTest()){
                        System.debug('Successfully updated opportunity ' + sr.getId() + ' which was created because of the converted lead.');
                    } else if(!sr.isSuccess() || Test.isRunningTest()){
                        //log error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.