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
goosegoose 

Triggers calling apex class not woring properly

Hi,

I'm trying to write a class that can be called from two separate triggers. It works for one but not the other and I cant see why:

I have a custom object - flight__c - that is a child object of accounts. One of the custom fields of flight__c is flight_hours and the trigger sums up all the flight hours of completed flights and puts the sum in a custom field of the parent account.

I couldn't use roll-up summary as the formula includes a filter on another of the flight custom fields.

The class is:

Code:
public class sumFlightHours {
   public static void getTotals(Account[] accs) {
      for (Account acc : accs) {
         Double tot = 0;
         for (Flight__c flight : [select id, Flight__c.Flight_hours__c from Flight__c where Flight__c.Flight_Account__c = :acc.id and Flight_status__c ='Flown']) {
            tot += flight.Flight_hours__c;
         }
         acc.Hours_used__c = tot;
      }
   }
}

 Now - with a trigger added to accounts, any changes to an account record will re-sum the flight records.

Code:
trigger updateHoursFlown on Account (before update, before insert) {
    sumFlightHours.getTotals(Trigger.new);
}

 
This works fine. (You might ask why is there such a trigger on accounts and not just on flights, but I plan to modify the sum later to include editable account fields in the formula so any changes in these will be needed).

However, a trigger added to flights does not work and I cant see why? This trigger pulls out the parent accounts for the affected flights and runs the same class method.

Code:
trigger updateParentHoursFlown on Flight__c (after update, after insert) {
Account[] accs = new Account[0];
for (Flight__c flight : Trigger.new) {
    for (Account acc : [select id from Account where id = :flight.Flight_Account__c]) {
        accs.add(acc);
        }
    }
sumFlightHours.getTotals(accs);
}

 
In this trigger, I use 'after' as I assume it needs the flights written to the database before the summation occurs. But there are no errors, it's just that the summary is not adjusted - not until editing the parent account when the other trigger does it's job for it.

Can anybody see what I've done wrong?

Thanks

SuperfellSuperfell
Your apex class is just updating the passed in account objects, that's fine for the first case, where the account objects, are from the trigger, so changes there will be updating there by the trigger process, but in the 2nd case, nothing applies you account changes back to the db.
goosegoose
Doh - thanks. I had previously added an "update acc" but recieved an error on saving a flight (the same I had when I had used update when the trigger was for account). I assumed it was for the same reason - but it was because the trigger on the flight save updated the account which fired off the account trigger which ran the "update acc" again and caused the error. I put a conditional around the update in the class and it's all OK now.
 
Thanks.