+ Start a Discussion
The new LearnerThe new Learner 

Trigger is not working , when the products are more than two

Hi Experts,
 
I have written a trigger, when the opportunity contain inactive product I need to throw an error, however, its working fine for one product, when it have more than one , when I am trying to delete it's not working its throwing an error by saying  "select correct product"
can anyone help me out? 
 
public static void products(Set<Id> oppIds, List<Opportunity> opplist)
    {
        
        List<OpportunityLineItem> opplineitems = [SELECT OpportunityId FROM OpportunityLineItem WHERE Product2.IsActive = false AND OpportunityId IN: OppIds];
        
        Set<Id> opptyId = New Set<Id>();
        for(OpportunityLineItem opplt:opplineitems)
        {
            
            opptyId.add(opplt.OpportunityId);
        }
        
        for (Opportunity opp :opplist) 
        {
            
            if(opptyId.contains(opp.Id))
            {
                
                opp.addError('select correct product');

            }
        }
            
    }

 
Suraj Tripathi 47Suraj Tripathi 47

Hi,

Try this below code.

trigger OpportunityTrigger on Opportunity (after undelete, before delete,before update,before insert,after insert) {
  
    
    
    if(trigger.isBefore && Trigger.isUpdate){
       
        
                List<OpportunityLineItem> opliList=new List<OpportunityLineItem>();
        opliList=[Select Id,Product2Id,OpportunityId from OpportunityLineItem where OpportunityId in: trigger.newMap.keySet()];
      Set<Id> setId=new Set<Id>();
        for(OpportunityLineItem opli:opliList){
            setId.add(opli.Product2Id);
        }
        
        List<Product2> productList=new List<Product2>([select Id,IsActive from Product2 where id in: setId and IsActive=false]);
        for(Opportunity op:trigger.new){
            if(productList.size()>0){
                op.addError('remove the inactive products from the opportunity ');
            }
        }
        // accessMapData.recordsUpdate(trigger.newMap,trigger.oldMap);
    }
    
    if(trigger.isBefore && Trigger.isDelete){
                        List<OpportunityLineItem> opliList=new List<OpportunityLineItem>();
        opliList=[Select Id,Product2Id,OpportunityId from OpportunityLineItem where OpportunityId in: trigger.oldMap.keySet()];
      Set<Id> setId=new Set<Id>();
        for(OpportunityLineItem opli:opliList){
            setId.add(opli.Product2Id);
        }
        
        List<Product2> productList=new List<Product2>([select Id,IsActive from Product2 where id in: setId and IsActive=false]);
        for(Opportunity op:trigger.new){
            if(productList.size()>0){
                op.addError('remove the inactive products from the opportunity ');
            }
        }
    }
}

Please mark it as the Best Answer If it helps you

Thank You

The new LearnerThe new Learner
hi suraj,

same error , not deleting
veer soniveer soni
hy new learner,
yes, your trigger is working fine. it gives error because you are firing on delete also.
If you want that error is not throw on delete event then remove trigger.delete functionality like below.
 
trigger findInActiveProductOnOpportuntiy on Opportunity (before update) {
    
    if(trigger.isBefore){
        if(trigger.isUpdate){
            set<string> setOppIds = new set<string>();
            for(Opportunity Opp : trigger.new){
                if(Opp.Id != null){
                    setOppIds.add(Opp.Id);
                }
            }
            
            for(OpportunityLineItem oLI : [SELECT OpportunityId,Product2Id,Product2.IsActive FROM OpportunityLineItem 
                                           WHERE Product2.IsActive = false 
                                           AND OpportunityId IN: setOppIds]){
                                               if(trigger.newMap.containsKey(oLI.OpportunityId)){
                                                   trigger.newMap.get(oLI.OpportunityId).addError('select correct product'); 
                                               continue;
                                               }  
                                           
            }
            
            
        }
        
        
        
    }
    
   
}


above trigger only work when you will update opportunity. delete will not effect on it.
let me know if it helps you and marking it as best Answer.
Thank you
Suraj Tripathi 47Suraj Tripathi 47
opportunity has 2 opportunitylineItems and both are inactive then I am able to delete both opportunitylineItems