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);
}
}