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
uptime_andrewuptime_andrew 

Trigger After Delete - Getting Error

I have a custom object that I am trying to execute a trigger on after insert, update, and delete.   The trigger functions as expected for the insert and update cases, but I'm getting an error when trying to execute it after delete (I tried changing to before delete as well, and got the same error).

The trigger is below.  I get the error at line "for (MyObject thisObject : System.Trigger.new) {", so I imagine my syntax must be incorrect here for the "delete" case.  However, I've had difficulty finding an example to refer to in the Force.com Cookbook or by searching Salesforce.com.  Any insight would be appreciated.

Here is my trigger:

trigger MyTrigger on MyObject (after insert, after update, after delete) {

    Map<String, MyObject> soMap = new Map<String, MyObject>();

    for (MyObject thisObject : System.Trigger.new) {
        Decimal Total = 0;
        Long Count = 0;

        // First, check for records meeting criteria A
        for (MyObject obj : [ SELECT Amount from MyObject where Account__c = :thisObject.Account__c  and fieldA = 'A']) {
            // sum the amounts
            Total += obj.Amount;
            Count++;
        }           
   
        // if no records exist, look for criteria B
        if (Count == 0) {
            for (MyObject obj : [ SELECT Amount from MyObject where fieldA = 'B' and CreatedDate = LAST_N_DAYS:365 AND Account__c = :thisObject.Account__c  ]) {
                // total the amounts
                Total += obj.Amount;
            }           
           
            // get 50% of the total
            Total = 0.5 * Total;
        }      

        // update value
        for (Account a : [select id from Account where id = :thisObject.Account__c]) {
            a.customField = Total;
            update a;
        }
    }
}


Best Answer chosen by Admin (Salesforce Developers) 
baionesbaiones
The context variable Trigger.new is only available in "after insert" and "after update" triggers. Check out the "Trigger context variables" section in the Apex Developer's Guide. You probably want to use System.Trigger.old for the "after delete" case

All Answers

baionesbaiones
The context variable Trigger.new is only available in "after insert" and "after update" triggers. Check out the "Trigger context variables" section in the Apex Developer's Guide. You probably want to use System.Trigger.old for the "after delete" case

This was selected as the best answer
uptime_andrewuptime_andrew
Thank you baiones - System.Trigger.old was what I was missing.  Problem solved!
sandeep@Salesforcesandeep@Salesforce

Trigger.New is now available in Trigger for event after Delete

KamsR_DEVKamsR_DEV
This discussion helps me a lot. I too missed trigger.old for deletion trigger.

Thanks for that.