0

So I was trying to expand on this budget app I made for a project. The Budget App is pretty basic having a Transaction object that has a master-detail relationship with a category object which in turn has a master-detail relationship with a budget.

My Category has a roll-up summary that gathers transaction amounts and sums it up. And the Budget in turn has a roll-up summary that collects the data from the categories.(I had to assign hidden variables that would be updated to equal the amount of the roll-up summary since you can't perform a roll-up summary on a roll-up summary).

The transactions have a date/time on them to mark when the transaction took place, as well as a checkbox that is updated to see if the transaction is in the same month and year as the budget's selected month/year (it won't always be the current month). There is a date/time variable on the budget to make this comparison and it works fine when I update the transaction.

However, I ran into an issue with attempting to update all the transactions when I change the selected month/year on the parent.

I attempted to use a flow to update all the transactions so they would run their process builder 'on-update check month/year' process. But I couldn't get it to work.

I attempted to use triggers and from what I can tell, It should work. But it fails to actually update any records.

Maybe i'm missing something obvious but this was my attempt.

trigger BudgetTrigger on Budget__c (before insert, before update, after insert, after update) {
    if (Trigger.isBefore){
        BudgetTriggerHandler.RefreshTransactions(Trigger.new);
    }
} 

public class BudgetTriggerHandler {
    public static void RefreshTransactions(List<Budget__c> Budget){

        for(Budget__c b : Budget){
            datetime selectedDate = b.Select_Budget_Month_Year_Date__c;
            List<Category__c> Cat = [Select id 
                                     From Category__c
                                     Where Budget_ID__c = :b.id]; 
            for(Category__c c : Cat){
                List<Transaction__c> Trans = [Select id, Date_Time__c
                                              From Transaction__c
                                              Where Category_ID__c = :c.id];
                for(Transaction__c T : Trans){
                    if(T.Date_Time__c.month() == selectedDate.month() && T.Date_Time__c.year() == selectedDate.year()){
                        T.Is_Budges_Month_Year__c = true;
                    } else {
                        T.Is_Budges_Month_Year__c = false;
                    }
                }
                database.update(Trans, false);
            }
        }


    }
}
1
  • Please avoid adding tags which have nothing to do with what you are asking. The [community] tag is for questions about the Communities feature.
    – Adrian Larson
    Commented Aug 28, 2019 at 3:21

1 Answer 1

0

first you can use the 'in' statement as a best practice . Never do a query inside a for loop statement

can you try doing a list of objects then you add em all? like this ;

public class BudgetTriggerHandler {
public static void RefreshTransactions(List<Budget__c> Budget){

    for(Budget__c b : Budget){
        datetime selectedDate = b.Select_Budget_Month_Year_Date__c;
        List<Category__c> Cat = [Select id 
                                 From Category__c
                                 Where Budget_ID__c = :b.id]; 
        for(Category__c c : Cat){
            List<Transaction__c> Trans = [Select id, Date_Time__c
                                          From Transaction__c
                                          Where Category_ID__c = :c.id];

            List<Transaction__c> TransToUpdate = new List<Transaction__c>();
            for(Transaction__c T : Trans){
                if(T.Date_Time__c.month() == selectedDate.month() && T.Date_Time__c.year() == selectedDate.year()){
                    T.Is_Budges_Month_Year__c = true;

                } else {
                    T.Is_Budges_Month_Year__c = false;
                }
             TransToUpdate.add(T);
            }
            update TransToUpdate;
        }
    }
  }
}

Question: Is there an error? Based from your code , aren't you hitting the Governor Limits?

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .