Saturday, 30 November 2013

Roll Up Summary Fields using Trigger After events

Roll Up Summary Fields using Trigger After events :

trigger My_MonthsRollUp_US on THD_Sales__c (after insert, after update, after delete, after undelete) {
    Map<Id,Account> updateAccounts = new Map<Id,Account>();
    Set<Id> updateAccountIds = new Set<Id>();    
  // If we are inserting, updating, or undeleting, use the new ID values
  if(Trigger.isInsert || Trigger.isUpdate || Trigger.isUndelete) {
    if(trigger.new != null)
        for(THD_Sales__c thdsales:Trigger.new)
            if(thdsales.SFDC_Account_ID__c != '0017000000ZWk80AAD')
              updateAccounts.put(thdsales.SFDC_Account_ID__c,null);
  }
  // If we are updating, some accounts might change, so include that as well as deletes
  if(Trigger.isUpdate || Trigger.isDelete) {
    if(trigger.old != null)
        for(THD_Sales__c thdsales:Trigger.old)
            if(thdsales.SFDC_Account_ID__c != '0017000000ZWk80AAD')
              updateAccounts.put(thdsales.SFDC_Account_ID__c,null);
  }
  // Do not create a record for null field
  updateAccounts.remove(null);  
  // Create in-memory copies for all accounts that will be affected
  for(Id accountId:updateAccounts.keyset()) 
    updateAccounts.put(accountId,new Account(id=accountId,
    Total_My_SO_Sales_January__c=0, Total_My_SO_Sales_February__c=0,Total_My_SO_Sales_March__c=0,
    Total_My_SO_Sales_April__c=0,Total_My_SO_Sales_May__c=0,Total_My_SO_Sales_June__c=0,
    Total_My_SO_Sales_July__c=0,Total_My_SO_Sales_August__c=0,Total_My_SO_Sales_September__c=0,
    Total_My_SO_Sales_October__c=0,Total_My_SO_Sales_November__c=0,Total_My_SO_Sales_December__c=0,
    Total_Cash_Carry_Sales_January__c=0, Total_Cash_Carry_Sales_February__c=0,Total_Cash_Carry_Sales_March__c=0,
    Total_Cash_Carry_Sales_April__c=0,Total_Cash_Carry_Sales_May__c=0,Total_Cash_Carry_Sales_June__c=0,
    Total_Cash_Carry_Sales_July__c=0,Total_Cash_Carry_Sales_August__c=0,Total_Cash_Carry_Sales_September__c=0,
    Total_Cash_Carry_Sales_October__c=0,Total_Cash_Carry_Sales_November__c=0,Total_Cash_Carry_Sales_December__c=0
    ));
    
    // Run an optimized query that looks for all accounts that meet the if/then criteria
    map< string, list< schema.sobjectfield > > fieldMap =
        new map< string, list< schema.sobjectfield > > {
            'Special Order' =>
                new list< schema.sobjectfield > {
                    null,
                    Account.Total_My_SO_Sales_January__c,
                    Account.Total_My_SO_Sales_February__c,
                    Account.Total_My_SO_Sales_March__c,
                    Account.Total_My_SO_Sales_April__c,
                    Account.Total_My_SO_Sales_May__c,
                    Account.Total_My_SO_Sales_June__c,
                    Account.Total_My_SO_Sales_July__c,
                    Account.Total_My_SO_Sales_August__c,
                    Account.Total_My_SO_Sales_September__c,
                    Account.Total_My_SO_Sales_October__c,
                    Account.Total_My_SO_Sales_November__c,
                    Account.Total_My_SO_Sales_December__c
                },
           'Cash & Carry' =>
                new list< schema.sobjectfield > {
                    null,
                    Account.Total_Cash_Carry_Sales_January__c,
                    Account.Total_Cash_Carry_Sales_February__c,
                    Account.Total_Cash_Carry_Sales_March__c,
                    Account.Total_Cash_Carry_Sales_April__c,
                    Account.Total_Cash_Carry_Sales_May__c,
                    Account.Total_Cash_Carry_Sales_June__c,
                    Account.Total_Cash_Carry_Sales_July__c,
                    Account.Total_Cash_Carry_Sales_August__c,
                    Account.Total_Cash_Carry_Sales_September__c,
                    Account.Total_Cash_Carry_Sales_October__c,
                    Account.Total_Cash_Carry_Sales_November__c,
                    Account.Total_Cash_Carry_Sales_December__c
                } 
        };     

   for(THD_Sales__c thdsales : [SELECT Id, Month_Year__c, POS_Order_Type__c, Amount__c, SFDC_Account_ID__c
   FROM  THD_Sales__c WHERE SFDC_Account_ID__c IN :updateAccounts.keySet( ) AND 
   POS_ORDER_TYPE__C IN ('Special Order','Cash & Carry') AND Month_Year__c != null]){ 
                            
        updateAccounts.get( thdsales.sfdc_account_id__c ).put(fieldmap.get( thdsales.pos_order_type__c )[thdsales.month_year__c.month()],
            ( Decimal )( updateAccounts.get( thdsales.sfdc_account_id__c ).get( fieldmap.get( thdsales.pos_order_type__c )[thdsales.month_year__c.month()]))+thdsales.Amount__c );
    }
  // Update all the accounts with new values.
  Database.update(updateAccounts.values());
}

Labels