Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- trigger LAM_ChildCount on Account (after insert, after update, before delete, after undelete) {
- Set<Id> Ids= new Set<Id>();
- List<Account> acclist = new List<Account>();
- Integer count = 0;
- if(Trigger.isInsert || Trigger.isUpdate){
- for(Account acc: Trigger.new){
- if(acc.ParentId!=null)
- Ids.add(acc.ParentId);
- acclist.add(acc);
- }
- }
- if(Trigger.isUpdate){
- for(Account acc: Trigger.old){
- if(acc.ParentId!=null)
- Ids.add(acc.ParentId);
- acclist.add(acc);
- }
- }
- if(Ids.size()>0){
- List<Account> accChild = new List<Account>([SELECT Id,ParentId FROM Account WHERE ParentId IN: Trigger.newMap.keySet()]);
- List<Account> accParent = new List<Account>([SELECT Id, Retail_Child_Accounts__c FROM Account WHERE Id IN: Trigger.newMap.keySet()]);
- for(Account ac: accParent){
- count =0;
- for(Account acChild: accChild){
- if(acChild.ParentId == ac.Id)
- count++;
- }
- ac.Retail_Child_Accounts__c = count;
- }
- try{
- upsert accParent;
- }catch(DMLException ex){
- System.debug('Exception is '+ex);
- }
- }
- }
- public class LAM_LRE_Rollup{
- /*
- First step is to create a context for LREngine, by specifying parent and child objects and
- lookup relationship field name
- */
- public static Id LAMCustomerRecordType = Schema.SObjectType.Account.getRecordTypeInfosByName().get('LAM Customer Account').getRecordTypeId();
- public static Id LAMDivisionRecordType = Schema.SObjectType.Account.getRecordTypeInfosByName().get('LAM Division Account').getRecordTypeId();
- public static Id LAMSiteRecordType = Schema.SObjectType.Account.getRecordTypeInfosByName().get('LAM Site Account').getRecordTypeId();
- public static Id LAMEnterpriseRecordType = Schema.SObjectType.Account.getRecordTypeInfosByName().get('LAM Enterprise Account').getRecordTypeId();
- //public static Id DukeEnergyStandardAccounts = Schema.SObjectType.Account.getRecordTypeInfosByName().get('DukeEnergyStandardAccounts').getRecordTypeId();
- //'012700000009H1Y'
- public static void rollupData(List<account> lstAcct)
- {
- List<Account> lstAccts = new List<account>();
- for(Account a : lstAcct)
- {
- if(a.recordtypeid != LAMEnterpriseRecordType && a.parent_Account_for_lam__c != null && (a.recordtypeid == LAMCustomerRecordType || a.recordtypeid == LAMSiteRecordType || a.RecordTypeId == LAMDivisionRecordType /*|| a.RecordTypeId == DukeEnergyStandardAccounts*/) && a.Account_Status__c == 'Active') //put an and clause here with nested OR for Rollup. E.g. && (a.recordtypeid == enterprise || a.recordtypeid == site)
- {
- lstAccts.add(a);
- system.debug(a);
- }
- }
- LREngine.Context ctx = new LREngine.Context(Account.SobjectType, // parent object
- Account.SobjectType, // child object
- Schema.SObjectType.Account.fields.Parent_Account_for_LAM__c// relationship field name
- );
- /*
- Next, one can add multiple rollup fields on the above relationship.
- Here specify
- 1. The field to aggregate in child object
- 2. The field to which aggregated value will be saved in master/parent object
- 3. The aggregate operation to be done i.e. SUM, AVG, COUNT, MIN/MAX
- */
- ctx.add(
- new LREngine.RollupSummaryField(
- Schema.SObjectType.Account.fields.AnnualRevenue,
- Schema.SObjectType.Account.fields.AnnualRevenue,
- LREngine.RollupOperation.Sum
- ));
- ctx.add(
- new LREngine.RollupSummaryField(
- Schema.SObjectType.Account.fields.Rolling_12_Month_Total_CCF__c,
- Schema.SObjectType.Account.fields.Rolling_12_Month_Total_CCF__c,
- LREngine.RollupOperation.Sum
- ));
- ctx.add(
- new LREngine.RollupSummaryField(
- Schema.SObjectType.Account.fields.Rolling_12_Month_Total_Usage_kWh__c,
- Schema.SObjectType.Account.fields.Rolling_12_Month_Total_Usage_kWh__c,
- LREngine.RollupOperation.Sum
- ));
- ctx.add(
- new LREngine.RollupSummaryField(
- Schema.SObjectType.Account.fields.Rolling_12_Month_Total_Revenue__c,
- Schema.SObjectType.Account.fields.Rolling_12_Month_Total_Revenue__c,
- LREngine.RollupOperation.Sum
- ));
- ctx.add(
- new LREngine.RollupSummaryField(
- Schema.SObjectType.Account.fields.Account_Max_Demand_kw__c,
- Schema.SObjectType.Account.fields.Account_Max_Demand_kw__c,
- LREngine.RollupOperation.Sum
- ));
- /*
- Calling rollup method returns in memory master objects with aggregated values in them.
- Please note these master records are not persisted back, so that client gets a chance
- to post process them after rollup
- */
- Sobject[] masters = LREngine.rollUp(ctx, lstAccts);
- // Push the changes in master
- update masters;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement