You need to sign in to do that
Don't have an account?
Trigger to replace a report formula calculation
Hi All,
I'm working on a trigger and could use some help. I've hit a report formula limitation and am trying to recreate the same logic in a trigger. The basic requirement is to collect a field value from child objects, run a simple calculation, then assign that value to a field on the parent account. From the debug statemtents, it seems that things seem to fall apart at line 55. Any feedback would be appreciated!
Thank you!
I'm working on a trigger and could use some help. I've hit a report formula limitation and am trying to recreate the same logic in a trigger. The basic requirement is to collect a field value from child objects, run a simple calculation, then assign that value to a field on the parent account. From the debug statemtents, it seems that things seem to fall apart at line 55. Any feedback would be appreciated!
Thank you!
trigger JobsIncomeCalc on Form__c (after insert, after update, after delete) { //Forms List List <Form__c> FormsList = [SELECT id, Account__c, Account__r.id, X2_1_a_Total_of_jobs_Year_1__c, X2_1_a_Total_of_jobs_Year_2__c FROM form__c]; //Distinguish whether the list should act on insert, update or delete if(trigger.isInsert || trigger.isUpdate){ FormsList = Trigger.New; } if(trigger.isDelete){ FormsList = Trigger.Old; } //Account Ids Set Set <id> accountIds = New Set <id>(); //Add form account ids to set for(Form__c F: FormsList){ accountIds.add(f.Account__c); System.debug('account id'+f.Account__c); } //Map to pair accounts and forms Map<id,Double> formAccountMap= New Map<id,Double>(); for(form__c f: [SELECT id, Account__r.id, Account__r.X2_1_a_Total_of_Jobs_Hist_Cumulative__c FROM Form__c WHERE Account__r.id IN: accountIds]){ Double AccTotalJobsHist = f.Account__r.X2_1_a_Total_of_Jobs_Hist_Cumulative__c; formAccountMap.put(f.Account__r.ID, AccTotalJobsHist); system.debug('AccTotalJobsHist='+ AccTotalJobsHist); system.debug('Account total jobs from form map='+ formAccountMap.get(f.Account__r.ID)); } //Accounts to update list List<Account> AcctsToUpdate = new List<Account>(); system.debug('AcctsToUpdate='+AcctsToUpdate); //Apply calculation to all forms in FormList for(Account a: AcctsToUpdate){ Double AccTotalJobsHist = formAccountMap.get(a.ID); system.debug('AccTotalJobsHist='+AccTotalJobsHist); system.debug('AccTotalJobsHist='+formAccountMap.get(a.ID)); Double TotalJobsY1 = a.form__r.X2_1_a_Total_of_jobs_Year_1__c; Double TotalJobsY2 = a.form__r.X2_1_a_Total_of_jobs_Year_2__c; system.debug('TotalJobsY1='+TotalJobsY1); system.debug('TotalJobsY2='+TotalJobsY2); Double CalculatedTotalJobs = (TotalJobsY2 - TotalJobsY1); system.debug('CalculatedTotalJobs='+CalculatedTotalJobs); //Set calculated value in account field AccTotalJobsHist = CalculatedTotalJobs; AcctsToUpdate.add(a); } Update AcctsToUpdate; }
The code below should do what you are trying to do.
NOTE: This code has not been tested and may contain typographical or logical errors
All Answers
When a Form is inserted, all of the forms for the account that it is inserted for should have it's total jobs for year 1 added together and then it's total jobs for year 2 added together and then those two subtracted and stored on the account?
Can you tell me why 4-9 are problematic? The soql is not in the for loop? I see the problem with iterating over the empty list -- that is very helpful and my biggest hang up right now. How I can I fix it? I can't iterate accounts over trigger.new and got an error when I tried to poplulate the list with a soql statemnt where account id is in accountIds.
Thanks!
The code below should do what you are trying to do.
NOTE: This code has not been tested and may contain typographical or logical errors
Thanks again!
But now I seem to be writing over my diffYr21 variables with each iteration over form records. What I really need is something like
I fell like I'm not utilizing the map properly to isolate each recrod individually.