+ Start a Discussion
Amit Vyas 8Amit Vyas 8 

I want to delete my related records once Opportunitylineitem records are deleted using trigger

I have written a trigger to delete my custom object records which have OLI lookup field for OpportunityLineItems once LineItem records are deleted.

trigger CompetitorRecord on OpportunityLineItem(after delete){
if(Trigger.isdelete){
                CompetitorRecordHandler.DeleteOpportunityCompetitor(Trigger.old);
            }
}

My handler class is:
public static void DeleteOpportunityCompetitor(List<OpportunityLineItem> newList){
        
         List<id> listIds = new List<id>(); 
        
        for(opportunityLineItem oli : newList){
            listIds.Add(oli.id);
        }
        system.debug('listIds'+listIds);
        List<Opportunity_Competitor__c> ocList = new List<Opportunity_Competitor__c>([SELECT id FROM Opportunity_Competitor__c WHERE Opportunity_Product__c IN :listIds]);
        if(!ocList.isEmpty()){
            delete ocList;
        }
    }

But the trigger is not working can someone help me to know if I'm missing something.
Best Answer chosen by Amit Vyas 8
Suraj Tripathi 47Suraj Tripathi 47
Hi Amit,

You can use before delete instead of the after delete.
trigger CompetitorRecord on OpportunityLineItem(before delete){
if(Trigger.isdelete){
                CompetitorRecordHandler.DeleteOpportunityCompetitor(Trigger.old);
            }
}
 
public static void DeleteOpportunityCompetitor(List<OpportunityLineItem> newList){
        
         List<id> listIds = new List<id>(); 
        
        for(opportunityLineItem oli : newList){
            listIds.Add(oli.id);
        }
        system.debug('listIds'+listIds);
        List<Opportunity_Competitor__c> ocList = new List<Opportunity_Competitor__c>([SELECT id FROM Opportunity_Competitor__c WHERE Opportunity_Product__c IN :listIds]);
        if(!ocList.isEmpty()){
            delete ocList;
        }
    }
In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 

 

All Answers

Suraj Tripathi 47Suraj Tripathi 47
Hi Amit,

You can use before delete instead of the after delete.
trigger CompetitorRecord on OpportunityLineItem(before delete){
if(Trigger.isdelete){
                CompetitorRecordHandler.DeleteOpportunityCompetitor(Trigger.old);
            }
}
 
public static void DeleteOpportunityCompetitor(List<OpportunityLineItem> newList){
        
         List<id> listIds = new List<id>(); 
        
        for(opportunityLineItem oli : newList){
            listIds.Add(oli.id);
        }
        system.debug('listIds'+listIds);
        List<Opportunity_Competitor__c> ocList = new List<Opportunity_Competitor__c>([SELECT id FROM Opportunity_Competitor__c WHERE Opportunity_Product__c IN :listIds]);
        if(!ocList.isEmpty()){
            delete ocList;
        }
    }
In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 

 
This was selected as the best answer
Amit Vyas 8Amit Vyas 8
Thanks a Lot It worked :)
Amit Vyas 8Amit Vyas 8
Hi Suraj,
In the same trigger I'm inserting records related to Opportunity Line Item once a Line Item is added to opportunity but I'm not able to sorf that insert list based on custom field. can you halp me with this?

Trigger is :
if(Trigger.isInsert){
                CompetitorRecordHandler.CreateRelatedCompetitor(Trigger.new);
            }

Handler class is below:
public static void CreateRelatedCompetitor(List<OpportunityLineItem> newList){
        
        List<Opportunity_Competitor__c> ocList = new List<Opportunity_Competitor__c>();
        
        for(OpportunityLineItem oli : newList){
            Opportunity_Competitor__c OppComp= new Opportunity_Competitor__c();
            
            OppComp.Forecast_Date__c=String.ValueOf(oli.ServiceDate);
            OppComp.Opportunity_Product__c=oli.Id;
            OppComp.Opportunity__c=oli.OpportunityId;
            OppComp.Product__c=oli.Product_Name__c;
            
            ocList.add(OppComp);
        }
        if(!ocList.isEmpty()){
            ocList.sort();
            insert ocList;
        }

I want to Sort my "ocList" on "Forecast_Date__c" basis.