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
Force.platformForce.platform 

after delete trigger

Hi all,
I want to update CustomAmmount(Custom field) on parent(Opportunity) when child(OpportunityLineItem) updated
but its not working in after delete event
Trigger:
trigger UpdateCustomAmmount on OpportunityLineItem (after insert, after update, after Undelete,after delete) {
    if(Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate || Trigger.isUnDelete))
    {
    UpdateCustomAmmount_Handler.updateAmmount(Trigger.new);
    }
    
   /if(Trigger.isAfter ||Trigger.isDelete)
    {
      UpdateCustomAmmount_Handler.updateAmmount(Trigger.old);  
    }
}

Contoller:
public class UpdateCustomAmmount_Handler {
    public static void updateAmmount(List<OpportunityLineItem> olt)
    {
       
        List<Id> listIds = new List<Id>();
        system.debug('list of opp ids---'+listIds);
        
        for (OpportunityLineItem childItem : olt) {
            listIds.add(childItem.OpportunityId);
        }
        
          list<Opportunity> parentOpp = new List<Opportunity>([SELECT id, Custom_Ammount__c, Name,(SELECT ID, TotalPrice FROM OpportunityLineItems) FROM Opportunity WHERE ID IN :listIds]);
          system.debug('list of opp with child---'+parentOpp); 
          
        for(Opportunity opp:parentOpp)
               {
                   opp.Custom_Ammount__c = 0;
            for(OpportunityLineItem item:opp.OpportunityLineItems)
            {
               
                opp.Custom_Ammount__c += item.TotalPrice; 
               }
            }
        If(parentOpp.size()>0){
            update parentOpp;  
        }    
    }
}
 
sfdcMonkey.comsfdcMonkey.com
hi, update your trigger and handler with below code :
trigger UpdateCustomAmmount on OpportunityLineItem (after insert, after update, after Undelete,after delete) {
    if(Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate || Trigger.isUnDelete))
    {
    UpdateCustomAmmount_Handler.updateAmmount(Trigger.new);
    }
    
   if(Trigger.isAfter && Trigger.isDelete){
      UpdateCustomAmmount_Handler.updateAmmount(Trigger.old);  
    }
}
public class UpdateCustomAmmount_Handler {
    public static void updateAmmount(List<OpportunityLineItem> olt){
       
        List<Id> listIds = new List<Id>();
        system.debug('list of opp ids---'+listIds);
        
        for (OpportunityLineItem childItem : olt) {
            listIds.add(childItem.OpportunityId);
        }
        
          list<Opportunity> parentOpp = new List<Opportunity>([SELECT id, Custom_Ammount__c, Name,(SELECT ID, TotalPrice FROM OpportunityLineItems) FROM Opportunity WHERE ID IN :listIds]);
          system.debug('list of opp with child---'+parentOpp); 
          
        for(Opportunity opp:parentOpp){
            opp.Custom_Ammount__c = 0;
            for(OpportunityLineItem item:opp.OpportunityLineItems){
			    Decimal tempPrice = (item.TotalPrice != null) ? item.TotalPrice : 0 ;	
                opp.Custom_Ammount__c += tempPrice; 
               }
            }
        If(parentOpp.size()>0){
            update parentOpp;  
        }    
    }
}
i hope it helps you.
   kindly Let me inform if it helps you and close your query by choosing best answer if you got your right answer so it can helps others
thanks
sfdcmonkey.com

 
Force.platformForce.platform
Hi piyush,
                 its not working
 
sfdcMonkey.comsfdcMonkey.com
what error you are getting with above code ? or what issue you have face on after delete ?
Force.platformForce.platform
I am getting following error:
Apex trigger UpdateCustomAmmount caused an unexpected exception, contact your administrator: UpdateCustomAmmount: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Class.UpdateCustomAmmount_Handler.updateAmmount: line 9, column 1  
sfdcMonkey.comsfdcMonkey.com
have you using same code ? which i provided, i have validate it on my dev org and it working perfect.
 
Prem Kumar Gupta 9Prem Kumar Gupta 9
trigger.new will not return anything as it doesn't know the value after delete and considered as Nul exception.
It should be before delete event and trigger.old