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
Cameron HouserCameron Houser 

Bug with process Builder, wont total sum of fields on parent object unless a new record is created on child object

Bug with process Builder, wont total sum of fields on parent object unless a new record is created on child object. 

I am tying to sum totals of records with a specific date onto a Account Field from an Order object records. My apex works, the only catch is I have to create 2 order records inorder to see the 1st records amount added to the field on the Account object. 

Any help would be greatly appriciated. 
Best Answer chosen by Cameron Houser
Cameron HouserCameron Houser
The solution was to use the Test.isRunningTest like so: 
if(!Test.isRunningTest()){
               SumOrdersDate = date.today();
               
            } else{
                SumOrdersDate = date.newInstance(2018, 09, 02);
               
            }

All Answers

Cameron HouserCameron Houser
I am trying to solve an issue with Process Builder working with an Apex class. The issue I am having is that the field I am updating will not update until I enter a second update, and then I only see the first update in action.
User-added image
My Apex Class:
public class SumOrdersOnAccountforNextYear {
    @InvocableMethod
    public static void SumAccountOrders(List<Id> AccountIds)
    {
        for(Id record: AccountIds) {
            Account updateAccount = new Account(Id = record);
            
            updateAccount.Last_Season_Sales__c = null;
            updateAccount.Current_Season_Total_Orders__c = null;
            updateAccount.Total_Sales_2_Years_Ago__c = null;
           
            for(AggregateResult result: [SELECT SUM(TotalAmount) Amt, AccountId Id FROM Order WHERE AccountId = :record AND (EffectiveDate >= :date.parse('07/01/2017')) AND (EffectiveDate <= :date.parse('06/30/2018')) GROUP BY AccountId]) {
                updateAccount.Last_Season_Sales__c = (Decimal)result.get('Amt');
            }
            for(AggregateResult result: [SELECT SUM(TotalAmount) Amt, AccountId Id FROM Order WHERE AccountId = :record AND (EffectiveDate >= :date.parse('07/01/2018')) AND (EffectiveDate <= :date.parse('06/30/2019')) GROUP BY AccountId]) {
                updateAccount.Current_Season_Total_Orders__c = (Decimal)result.get('Amt');
            }
            for(AggregateResult result: [SELECT SUM(TotalAmount) Amt, AccountId Id FROM Order WHERE AccountId = :record AND (EffectiveDate >= :date.parse('07/01/2016')) AND (EffectiveDate <= :date.parse('06/30/2017')) GROUP BY AccountId]) {
                updateAccount.Total_Sales_2_Years_Ago__c = (Decimal)result.get('Amt');
            }
            
            update updateAccount;
        }
    }
}
Cameron HouserCameron Houser
The solution was to use the Test.isRunningTest like so: 
if(!Test.isRunningTest()){
               SumOrdersDate = date.today();
               
            } else{
                SumOrdersDate = date.newInstance(2018, 09, 02);
               
            }
This was selected as the best answer