- Identifying if the lead is converted
- Which opportunity it is getting converted to. --> ConvertedOpportunityId
- Query on all the products added at lead related list.
- Create same products at Opportunity.
I want help with step 4, in adding the products to opportunity. How can I acheive that?
trigger AddProductsfromLead2Opp on Lead (after update) {
Set<Id> OppId = new Set<ID>();
List<Opportunity> Opp2Update = new List<Opportunity>();
List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();
List<Lead_Product__c> lp = new List<Lead_Product__c>();
//check if the lead is created
for(Lead l:Trigger.new){
/* Getting Opportunity Ids from the Lead after conversion .
ConvertedOpportunityId in Lead contains the Opportunity Id to which the Lead is converted.
*/
if(l.isConverted){
OppId.add(l.ConvertedOpportunityId);
}
if(OppId.size() > 0){
lp = [select id,Product__c, Lead__c from Lead_Product__c where Lead__c = :l.id];
system.debug(lp);
}
}
}