Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.33 KB | None | 0 0
  1. trigger LAM_ChildCount on Account (after insert, after update, before delete, after undelete) {
  2. Set<Id> Ids= new Set<Id>();
  3. List<Account> acclist = new List<Account>();
  4. Integer count = 0;
  5.  
  6. if(Trigger.isInsert || Trigger.isUpdate){
  7. for(Account acc: Trigger.new){
  8. if(acc.ParentId!=null)
  9. Ids.add(acc.ParentId);
  10. acclist.add(acc);
  11. }
  12. }
  13.  
  14. if(Trigger.isUpdate){
  15. for(Account acc: Trigger.old){
  16. if(acc.ParentId!=null)
  17. Ids.add(acc.ParentId);
  18. acclist.add(acc);
  19. }
  20. }
  21.  
  22. if(Ids.size()>0){
  23. List<Account> accChild = new List<Account>([SELECT Id,ParentId FROM Account WHERE ParentId IN: Trigger.newMap.keySet()]);
  24. List<Account> accParent = new List<Account>([SELECT Id, Retail_Child_Accounts__c FROM Account WHERE Id IN: Trigger.newMap.keySet()]);
  25. for(Account ac: accParent){
  26. count =0;
  27. for(Account acChild: accChild){
  28. if(acChild.ParentId == ac.Id)
  29. count++;
  30. }
  31. ac.Retail_Child_Accounts__c = count;
  32. }
  33. try{
  34. upsert accParent;
  35. }catch(DMLException ex){
  36. System.debug('Exception is '+ex);
  37. }
  38. }
  39. }
  40.  
  41. public class LAM_LRE_Rollup{
  42.  
  43. /*
  44. First step is to create a context for LREngine, by specifying parent and child objects and
  45. lookup relationship field name
  46. */
  47.  
  48. public static Id LAMCustomerRecordType = Schema.SObjectType.Account.getRecordTypeInfosByName().get('LAM Customer Account').getRecordTypeId();
  49. public static Id LAMDivisionRecordType = Schema.SObjectType.Account.getRecordTypeInfosByName().get('LAM Division Account').getRecordTypeId();
  50. public static Id LAMSiteRecordType = Schema.SObjectType.Account.getRecordTypeInfosByName().get('LAM Site Account').getRecordTypeId();
  51. public static Id LAMEnterpriseRecordType = Schema.SObjectType.Account.getRecordTypeInfosByName().get('LAM Enterprise Account').getRecordTypeId();
  52. //public static Id DukeEnergyStandardAccounts = Schema.SObjectType.Account.getRecordTypeInfosByName().get('DukeEnergyStandardAccounts').getRecordTypeId();
  53.  
  54. //'012700000009H1Y'
  55.  
  56.  
  57. public static void rollupData(List<account> lstAcct)
  58. {
  59. List<Account> lstAccts = new List<account>();
  60.  
  61. for(Account a : lstAcct)
  62. {
  63. 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)
  64. {
  65. lstAccts.add(a);
  66. system.debug(a);
  67. }
  68. }
  69. LREngine.Context ctx = new LREngine.Context(Account.SobjectType, // parent object
  70. Account.SobjectType, // child object
  71. Schema.SObjectType.Account.fields.Parent_Account_for_LAM__c// relationship field name
  72. );
  73. /*
  74. Next, one can add multiple rollup fields on the above relationship.
  75. Here specify
  76. 1. The field to aggregate in child object
  77. 2. The field to which aggregated value will be saved in master/parent object
  78. 3. The aggregate operation to be done i.e. SUM, AVG, COUNT, MIN/MAX
  79. */
  80. ctx.add(
  81. new LREngine.RollupSummaryField(
  82. Schema.SObjectType.Account.fields.AnnualRevenue,
  83. Schema.SObjectType.Account.fields.AnnualRevenue,
  84. LREngine.RollupOperation.Sum
  85. ));
  86.  
  87. ctx.add(
  88. new LREngine.RollupSummaryField(
  89. Schema.SObjectType.Account.fields.Rolling_12_Month_Total_CCF__c,
  90. Schema.SObjectType.Account.fields.Rolling_12_Month_Total_CCF__c,
  91. LREngine.RollupOperation.Sum
  92. ));
  93. ctx.add(
  94. new LREngine.RollupSummaryField(
  95. Schema.SObjectType.Account.fields.Rolling_12_Month_Total_Usage_kWh__c,
  96. Schema.SObjectType.Account.fields.Rolling_12_Month_Total_Usage_kWh__c,
  97. LREngine.RollupOperation.Sum
  98. ));
  99. ctx.add(
  100. new LREngine.RollupSummaryField(
  101. Schema.SObjectType.Account.fields.Rolling_12_Month_Total_Revenue__c,
  102. Schema.SObjectType.Account.fields.Rolling_12_Month_Total_Revenue__c,
  103. LREngine.RollupOperation.Sum
  104. ));
  105. ctx.add(
  106. new LREngine.RollupSummaryField(
  107. Schema.SObjectType.Account.fields.Account_Max_Demand_kw__c,
  108. Schema.SObjectType.Account.fields.Account_Max_Demand_kw__c,
  109. LREngine.RollupOperation.Sum
  110. ));
  111.  
  112.  
  113. /*
  114. Calling rollup method returns in memory master objects with aggregated values in them.
  115. Please note these master records are not persisted back, so that client gets a chance
  116. to post process them after rollup
  117. */
  118. Sobject[] masters = LREngine.rollUp(ctx, lstAccts);
  119.  
  120.  
  121. // Push the changes in master
  122.  
  123. update masters;
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement