+ Start a Discussion
Chinnu ChinnuChinnu Chinnu 

Develop an apex trigger to display the opportunity products in opportunity page

For example: you will have to create a field in opportunity (Added products - text field) should show product1; product2; product3;
Deepali KulshresthaDeepali Kulshrestha
Hi Chinnu,

Hope this will work fine for you,

Sample Code:--->
 
trigger AddRelatedProduct on Opportunity (after Update) 
{
    if(Trigger.isAfter && Trigger.isUpdate)
    {
        if(showRelatedProduct.flag==true)
        {
            showRelatedProduct.flag=false;
            showRelatedProduct.populateAddProductField(Trigger.New);
        }
    }
}

controller class

public class showRelatedProduct
{
    public static Boolean flag=true;
    
    public static void populateAddProductField(List<Opportunity> trigger_Opportunity)
    {
         try
        {
            List<OpportunityLineItem> getAllLineItems=[select id,OpportunityId,Product2Id,Product2.Name from OpportunityLineItem
                                                      where OpportunityId IN:trigger_Opportunity];
            System.debug('getAllLineItems-->'+getAllLineItems);
            Map<id,String> OpportunityMap=new Map<id,String>();
            Set<Id> OpportunityIds=new Set<Id>();
            
            if(getAllLineItems.size()>0)
            {
                for(OpportunityLineItem lineitem:getAllLineItems)
                {
                    OpportunityIds.add(lineitem.OpportunityId);
                    
                    if(OpportunityMap.containsKey(lineitem.OpportunityId))
                    {
                        String s=OpportunityMap.get(lineitem.OpportunityId);
                        s+=lineitem.Product2.Name+';';
                        OpportunityMap.put(lineitem.OpportunityId,s);
                        
                    }
                    else
                    {
                        OpportunityMap.put(lineitem.OpportunityId,'');
                        String s=OpportunityMap.get(lineitem.OpportunityId);
                        s+=lineitem.Product2.Name+';';
                        OpportunityMap.put(lineitem.OpportunityId,s);
                    }
                }//map created
                System.debug('OpportunityMap-->'+OpportunityMap);
                
                List<Opportunity> getOpportunity=[select id,Name,AddedProduct__c from Opportunity where Id IN:OpportunityIds];
                List<Opportunity> updateOpportunity=new List<Opportunity>();
                
                for(Opportunity opp:getOpportunity)
                {
                    opp.AddedProduct__c=OpportunityMap.get(opp.id);
                    
                    updateOpportunity.add(opp);
                }
                
                     if(updateOpportunity.size()>0)
                         update updateOpportunity;
                
            }
            
        }
        catch(Exception e)
        {
            System.debug('Error in--'+e.getLineNumber()+' Error is--'+e.getMessage());
        }
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com

 
Ajay K DubediAjay K Dubedi
Hi Chinnu

I have gone through your question and I have created a trigger that is working just fine in the case you defined.

//Apex Trigger -->
trigger AddOpportunityProductTrigger on Opportunity (before update) {
    if(Trigger.isBefore && Trigger.isUpdate) {
        AddOpportunityProductHandler.addProductNames(Trigger.new);                    
    }
}

//Trigger Handler Class -->
public class AddOpportunityProductHandler {
    public static void addProductNames(List<Opportunity> oppListNew) {
        List<OpportunityLineItem> oliList = new List<OpportunityLineItem>([Select Id, Product2.Name, OpportunityId From OpportunityLineItem Where OpportunityId In :oppListNew]);        
        Map<Id,List<OpportunityLineItem>>  oppMapWithLineItems = new Map<Id,List<OpportunityLineItem>>();
        for(OpportunityLineItem oli: oliList) {
            if(oppMapWithLineItems.ContainsKey(oli.OpportunityId)) {                    
                List<OpportunityLineItem> oliListTemp = new List<OpportunityLineItem>();
                oliListTemp = oppMapWithLineItems.get(oli.OpportunityId);
                oliListTemp.add(oli);
                oppMapWithLineItems.put(oli.OpportunityId, oliListTemp);
            } else {
                oppMapWithLineItems.put(oli.OpportunityId,new list<OpportunityLineItem>());
                List<OpportunityLineItem> oliListTemp = new List<OpportunityLineItem>();
                oliListTemp = oppMapWithLineItems.get(oli.OpportunityId);
                oliListTemp.add(oli);
                oppMapWithLineItems.put(oli.OpportunityId, oliListTemp);
            }
        }
        for(Opportunity opp: oppListNew) {
            opp.Added_Products__c = '';            
            for(OpportunityLineItem oli: oppMapWithLineItems.get(opp.Id)) {
                if(!(opp.Added_Products__c.contains(oli.Product2.Name))) {
                    opp.Added_Products__c += oli.Product2.Name+';';
                }      
            }
        }
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com