• baiones
  • NEWBIE
  • 35 Points
  • Member since 2008

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
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;
        }
    }
}


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;
        }
    }
}