+ Start a Discussion
HARSHIL PARIKH AKA:GovindHARSHIL PARIKH AKA:Govind 

Trigger doesn't allow me to delete the record once created.

Hello Developers!
I have a rollup summary sum trigger on child object and it works fine accept it doesn't allow me to delete the child record once created.
I appreciate your help!
Trigger DirectCareAmount on Direct_Care_Assistance__c( after insert, after update,after delete,after undelete) {
     Set<Id> DispoIdSet= new Set<Id>();
     List<Cat_Disposition__C > DispoListToUpdate = new List<Cat_Disposition__C>();
     Map<Id,Cat_Disposition__c>  MapDispoToReset  = new Map<Id,Cat_Disposition__c>();
     
    if(Trigger.isInsert || Trigger.isUpdate || Trigger.isUndelete)
    {
        for(Direct_Care_Assistance__c DC: Trigger.new)
        {
            if(DC.Cat_Disposition__C != null)
                {
                    DispoIdSet.add(DC.Cat_Disposition__C );
                }     
        }
    }
   If(Trigger.isDelete) 
      { 
       for(Direct_Care_Assistance__c DC: Trigger.old) 
           { 
               if(DC.Cat_Disposition__C != null) 
                   {     
                           DispoIdSet.add(DC.Cat_Disposition__C );
                           MapDispoToReset.put( DC.Cat_Disposition__C, new Cat_Disposition__C ( Id= DC.Cat_Disposition__C,
                                                                                           Money_Spent_on_Direct_Care_Assistance__c = 0 ) );
                   } 
           } 
       }
    
   for(AggregateResult res : [SELECT Count(id)Quantity, Cat_Disposition__C ,sum(Dollar_Amount__c)addition FROM Direct_Care_Assistance__c WHERE                                                                     
                                                                                   Cat_Disposition__C IN :DispoIdSet group by Cat_Disposition__C ]) 
        {
                DispoListToUpdate.add(new Cat_Disposition__C         (     Id=(Id)res.get('Cat_Disposition__C'), 
                                                                           Money_Spent_on_Direct_Care_Assistance__c =  (Double)res.get('addition')
                                                                     )     
                                     );
                                                        
               if(Trigger.IsDelete && MapDispoToReset.containsKey(  (Id)res.get('Cat_Disposition__C ') )  )
                    {
                            MapDispoToReset.remove(  (Id)res.get('Cat_Disposition__C')  );                  
                    }
            }
        for ( ID cid : MapDispoToReset.keySet() )
        {
          DispoListToUpdate.add((Cat_Disposition__C)MapDispoToReset.get(cid));
        }
        
    try
    {
      update DispoListToUpdate;
    }
    catch(DmlException de)
    {
      System.debug(de);
    }
}
Error:  when delete the child record:

Validation Errors While Saving Record(s)
There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger DirectCareAmount caused an unexpected exception, contact your administrator: DirectCareAmount: execution of AfterDelete caused by: System.SObjectException: Invalid field Cat_Disposition__C for AggregateResult: Trigger.DirectCareAmount: line 37, column 1". 


 
Best Answer chosen by HARSHIL PARIKH AKA:Govind
UC InnovationUC Innovation
Hi Govind,

Try removing the space after Cat_Disposition__c in the get call. Something like this:
 
MapDispoToReset.containsKey((Id)res.get('Cat_Disposition__c'))

Hope this helps!

AM
 

All Answers

UC InnovationUC Innovation
Hi Govind,

Try removing the space after Cat_Disposition__c in the get call. Something like this:
 
MapDispoToReset.containsKey((Id)res.get('Cat_Disposition__c'))

Hope this helps!

AM
 
This was selected as the best answer
HARSHIL PARIKH AKA:GovindHARSHIL PARIKH AKA:Govind
Thank you!!