1

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];
       if(!LstLeadRecd.isEmpty()){
       LeadRecd=LstLeadRecd[0];        
       LeadId = LeadRecd.Id;
       LeadRecd.Custom_ConvertedOpportunityId__c = opp.Id;
       LeadRecd.Ignore_Workflow__c = true;
       update LeadRecd;
        }    
    }

    if(!LstLeadRecd.isEmpty()){
    Database.LeadConvert lc = new database.LeadConvert();
    lc.setDoNotCreateOpportunity(true);
    lc.setLeadId(LeadId);
    lc.setAccountId(accountId);
    //lc.setOpportunityName(OpptyId);  //I am unable to do this.Are there any alternative? 
    lc.setConvertedStatus('4 New Opportunity'); 
    lc.setownerId(ownerId);
    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
2

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
0

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
        if(!leadConvertList.isEmpty()){
            //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++){
                    tempList.add(leadConvertList[j]);
                }
                //system.debug('tempList conversion = ' + tempList);
                Database.LeadConvertResult[] lcrList = Database.convertLead(tempList, false);
                for(Database.LeadConvertResult lcr : lcrList){
                    if(!lcr.isSuccess()){
                        //log internal error                            

                        //revert conversion checkboxes
                        Lead revLead = new Lead(id = lcr.getLeadId(), Convert_No_Opp__c = false, Convert_with_Opp__c = false);
                        revertList.add(revLead);
                    } else {
                        leadOppMap.put(lcr.getLeadId(),lcr.getOpportunityId());
                        leadContMap.put(lcr.getLeadId(),lcr.getContactId());
                    }
                }
                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
            if(updateOppList.size()>0){
                //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.