You need to sign in to do that
Don't have an account?

Apex Rollup Summary Trigger - more than one Sum in a map?
I've leveraged the trigger example here: http://www.anthonyvictorio.com/salesforce/roll-up-summary-trigger/ however I need to change it so that it can accomodate for 4 sum of currency fields (not just 1) to update four different rollup fields on the parent Account (Bank__c).
Some information: A Bank has multiple SRFs, and under each SRF are a set of Fees. There are 4 Fee fields on the SRFs that each need to roll up seperately onto their respective Fee fields on the Bank. That's why there is a Pipeline_Fees_Bank__c and Pipeline_Fees__c (SRF). There are 3 more sets of these fields.
How can I achieve rolling up the other three by modifying this?
trigger rollUpFees on Service_Request__c (after delete, after insert, after update) { //Limit the size of list by using Sets which do not contain duplicate elements set<Id> AccountIds = new set<Id>(); //When adding new SRFs or updating existing SRFs if(trigger.isInsert || trigger.isUpdate){ for(Service_Request__c p : trigger.new){ AccountIds.add(p.Bank__c); } } //When deleting SRFs if(trigger.isDelete){ for(Service_Request__c p : trigger.old){ AccountIds.add(p.Bank__c); } } //Map will contain one Bank Id to one sum value map<Id,Double> AccountMap = new map <Id,Double>(); //Produce a sum of fees on SRFs and add them to the map //use group by to have a single Bank Id with a single sum value for(AggregateResult q : [select Bank__c,sum(Pipeline_Fees__c) from Service_Request__c where Bank__c IN :AccountIds group by Bank__c]){ AccountMap.put((Id)q.get('Bank__c'),(Double)q.get('expr0')); } List<Account> AccountsToUpdate = new List<Account>(); //Run the for loop on Accounts using the non-duplicate set of Bank Ids //Get the sum value from the map and create a list of Accounts to update for(Account a : [Select Id, Pipeline_Fees_Bank__c from Account where Id IN :AccountIds]){ Double PaymentSum = AccountMap.get(a.Id); a.Pipeline_Fees_Bank__c = PaymentSum; AccountsToUpdate.add(a); } update AccountsToUpdate; }
To get an idea of what I'd like to change...There are 3 more "Fees" that need to be summed up:
//Produce a sum of fees on SRFs and add them to the map //use group by to have a single Bank Id with a single sum value for(AggregateResult q : [select Bank__c,sum(Pipeline_Fees__c),sum(Incurred_Fees__c),sum(Paid_Fees__c),sum(Unpaid_Fees__c) from Service_Request__c where Bank__c IN :AccountIds group by Bank__c]){ AccountMap.put((Id)q.get('Bank__c'),(Double)q.get('expr0')); }
I'm new to triggers, so I'm not sure how to achieve this! I don't want to be writing four triggers, one for each one - i dont think that's the best way to achieve this...
you will have to do the following changes in the code.
All Answers
I want to ask you a question here
what is the relation between Account object and Service_Request__c. is it a Master-Detail relationship or just a lookup relationship? Because you dont need a trigger in case if its a Master-Detail relation.
you will have to do the following changes in the code.
let me know if you face any issue with that
Thank you so much. Worked perfectly.
Having problems with my test class though....borrowed it from the link on the first post.
Pipeline_Fees__c on Service Requests (p1.Pipeline_Fees__c) is a formula field... i was reading that you needed to insert or update the record before formulas calculated. Order_Fees__c is a currency field that changes the Pipeline Fees formula.
Pipeline_Fees_Bank__c on the Account (a.Pipeline_Fees_Bank__c is a currency field). The trigger works great, but this test class is failing on the assert:
Assertion Failed: Expected 0.00, Actual: null - which i think is saying the Pipeline_Fees__c is null.
Where am i going wrong here?
Still pretty lost on the test class...
Hi SrikanthKuruva ,
I need to handle similar kind of scenerio, and it is working well with the help of Trigger except if I change master of child, amound is not deducted from old parent.
Two objects are there
1)Job
2)Campaign
Campaign is master of Job in lookup relationship and I want a field (like Rollup Summary) which will take sum of related jobs Amount.
eg.
Camp1 --
Job 1 -- amount = 10
Job 2 -- amount = 15
Job 3 -- amount= 20
so on campaign amount will be 45
but if i change the Campaign of Job 1 from Camp1 to another Campaign then its amount should be deducted from Camp1,
but this is not happening with my trigger,
Please help me on this,
here is my Trigger