function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Chinnu ChinnuChinnu Chinnu 

can any one please tell me how to write a test class for this code..

trigger OpportunityProduct on OpportunityLineItem (before insert,after insert, after update,after delete,after undelete) {
    //Setup the array to hold the Id to Iterate through
    Set<Id> CurrentOppId = new Set<Id>();
    List<string> prodName = new List<string>();
    Map<Id,Id> OppLineItemMap = new Map<Id,Id>();
    
    for (OpportunityLineItem OppLnItem : Trigger.isdelete?trigger.old:trigger.new){
        CurrentOppId.add(OppLnItem.OpportunityId);
        prodName.add(OppLnItem.Product2.name);
        OppLineItemMap.put(OppLnItem.opportunityId,OppLnItem.product2id);
    }
    
    if(trigger.isbefore){
        List<Opportunity> oppLst = [Select id,(select id,product2id,OpportunityId from opportunitylineitems) from opportunity where id=:CurrentOppId];
        for(opportunityLineitem opplit : trigger.new){
            for(opportunity oppt : oppLst){
                for(opportunityLineItem oppli : oppt.opportunitylineitems){
                    if(oppli.Product2Id == OppLineItemMap.get(oppli.OpportunityId)){
                        opplit.adderror('You Cant add the same OpportunityLineItem again');
                    }
                }
            }
        }
    }   
    
    // Create List of One Opportunity Id to Update
    List<Opportunity> OppId = [Select Id from Opportunity where Id in: CurrentOppId];
    
    // Create List of Opportunity Products to iterate through later
    List<OpportunityLineItem> relatedOLIs =
        [Select Id, OpportunityId, PricebookEntry.Product2.name from OpportunityLineItem 
         where OpportunityId in: CurrentOppId];
    
    List<Opportunity> Opplist = New List<Opportunity> ();
    //Iterate through the Opportunity to Update
    for (Opportunity opp2 : OppId){
        opp2.Product_Code_Selected__c = '';
        
        //Iterate through the line items to add PricebookEntryID data to the Opportunity
        for (OpportunityLineItem oli2 : relatedOLIs){
            opp2.Product_Code_Selected__c +=  oli2.PricebookEntry.Product2.name + '; ';                        
        }
        Opplist.add(opp2);
    }
    update Opplist;  
}