0
  1. Identifying if the lead is converted
  2. Which opportunity it is getting converted to. --> ConvertedOpportunityId
  3. Query on all the products added at lead related list.
  4. 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);
        }
    }
}
3
  • 1
    welcome to sfse. Note that your trigger is not bulkified as you are doing a SOQL query inside a for loop. Read up on Trailhead and in apex doc on how to use maps
    – cropredy
    Mar 2, 2022 at 19:04
  • Thank you cropredy. I will look into it. Meanwhile can you help me out with the above question ? How can i approach it ?
    – SF_newguy
    Mar 2, 2022 at 19:57
  • 1
    I suggest you learn how to bulkify this code first as it will provide insight into how to solve the requirement
    – cropredy
    Mar 2, 2022 at 20:58

0

You must log in to answer this question.

Browse other questions tagged .