• Vasu@blr
  • NEWBIE
  • 50 Points
  • Member since 2012

  • Chatter
    Feed
  • 2
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 19
    Questions
  • 10
    Replies

i was bulk testing this trigger and while it works in the ui i get this error:

 

"setParentFieldsFromChildTriggeronProgram: execution of BeforeInsert

caused by: System.ListException: Duplicate id in list: 006G000000JvcxjIAB

Trigger.setParentFieldsFromChildTriggeronProgram: line 27, column 1"

 

Here's the trigger.  

trigger setParentFieldsFromChildTriggeronProgram on Program__c (before insert, before update) 
{              
     List<Opportunity> oppList = new List<Opportunity>();
     Set<id> Ids = new Set<id>();
     for (Program__c prgm : Trigger.new) {
         Ids.add(prgm.Opportunity__c);
        }
     
     Map<id,Opportunity> oppMap = new Map<id,Opportunity>([Select Id,Product_Group__c,Product_Area__c,Product_Family__c,Product_Family_Child__c from Opportunity Where Id in :Ids]);  
     
     for (Program__c prgm : Trigger.new) 
     {
                 Opportunity o = oppMap.get(prgm.Opportunity__c);
                 o.Product_Group__c = prgm.Product_Group__c;
                 o.Product_Area__c = prgm.Product_Area__c;
                 o.Product_Family__c = prgm.Product_Family__c;
                 o.Product_Family_Child__c = prgm.Product_Family_Child__c;
                 oppList.add(o);
     }
                 update oppList;
     
}

 

Please help me how   can I improve my code.

 

My trigger is working when I tested it through user interface,

 

I tested my trigger by inserting 10 records using dataloader.

 

But, when I was inserting one lakh records with the batch size of 20. It is not working incase of insert.

but it is working for delete operation

 

My scenario:

 

I have three objects thdsales, commission and userbudget.

 

Userbudget (owner)

Commission (owner, Account number)

Thdsales (Account number)

 

I need to compare (date and channel type) which are common in userbudget and thdsales through commission.

If matches then I need to update a checkbox as true in userbudget.

 

Below is my trigger:  

 

for insert, instead of one month it is marking another month.(suppose instead of aug marking july record)

 

 

trigger MY_markAllCommissions_US on THD_Sales__c (before insert,after update,after delete) {
    
    if(Trigger.isinsert || Trigger.isupdate) {
        List<Commission__c> comilst = new List<Commission__c>();
        Set<Id> salesteamset = new Set<Id>();
        Set<Id> sfdcacids = new Set<Id>();
        Map<Id,User_Budget__c> usbdtmap = new Map<Id,User_Budget__c>();
        Map<Id,List<THD_Sales__c>> thdmap = new Map<Id,List<THD_Sales__c>>();
        for(THD_Sales__c thd : Trigger.new) {        
            sfdcacids.add(thd.SFDC_Account_ID__c);
            If(thdmap.get(thd.SFDC_Account_ID__c)==null) {
                List<THD_Sales__c> thdlst = new List<THD_Sales__c>();
                thdlst.add(thd);
                thdmap.put(thd.SFDC_Account_ID__c,thdlst);
            }
            else {
                thdmap.get(thd.SFDC_Account_ID__c).add(thd);
            }                      
        }
        //system.debug('$$$'+thdmap);
        comilst = [select id,Sales_Team_Member__c,Account__c from Commission__c where Account__c in :sfdcacids];  
        //System.debug('****'+comilst);        
        
        Map<Id,List<Commission__c>> cmmap = new Map<Id,List<Commission__c>>();
        for(Commission__c comi : comilst) {
            salesteamset.add(comi.Sales_Team_Member__c);
            if(cmmap.get(comi.Sales_Team_Member__c)==null) {
                List<Commission__c> cmlst = new List<Commission__c>();
                cmlst.add(comi);
                cmmap.put(comi.Sales_Team_Member__c,cmlst);
            }
            else {
                cmmap.get(comi.Sales_Team_Member__c).add(comi);
            }
        }
        //System.debug('dd11'+cmmap);
        List<User_Budget__c> ownerlst = [select OwnerId,Actual_Dirty__c,Channel__c,Month_Year__c from User_Budget__c where OwnerId in :salesteamset];
        for(User_Budget__c usbdt : ownerlst) {
        //system.debug('###'+thdmap.get(cmmap.get(usbdt.OwnerId).Account__c));
            for(Commission__c c : cmmap.get(usbdt.OwnerId)) {
            for(THD_Sales__c t : thdmap.get(c.Account__c)){
                if((t.POS_Order_Type__c == usbdt.Channel__c) &&
                t.Month_Year__c.year() == usbdt.Month_Year__c.year() &&
                t.Month_Year__c.month() == usbdt.Month_Year__c.month()) {
                    //usbdt.Channel__c = thdmap.get(cmmap.get(usbdt.OwnerId).Account__c)
                    User_Budget__c ub = new User_Budget__c();
                    usbdt.Actual_Dirty__c = True;
                    ub = usbdt;        
                    usbdtmap.put(ub.id,ub);
                }
            } 
            }           
        }    
        update usbdtmap.values();
    }
    
    if(Trigger.isdelete || Trigger.isupdate) {
         List<Commission__c> comilst = new List<Commission__c>();
        Set<Id> salesteamset = new Set<Id>();
        Set<Id> sfdcacids = new Set<Id>();
        Map<Id,User_Budget__c> usbdtmap = new Map<Id,User_Budget__c>();
        Map<Id,List<THD_Sales__c>> thdmap = new Map<Id,List<THD_Sales__c>>();
        for(THD_Sales__c thd : Trigger.old) {        
            sfdcacids.add(thd.SFDC_Account_ID__c);
            If(thdmap.get(thd.SFDC_Account_ID__c)==null) {
                List<THD_Sales__c> thdlst = new List<THD_Sales__c>();
                thdlst.add(thd);
                thdmap.put(thd.SFDC_Account_ID__c,thdlst);
            }
            else {
                thdmap.get(thd.SFDC_Account_ID__c).add(thd);
            }                      
        }
        //system.debug('$$$'+thdmap);
        comilst = [select id,Sales_Team_Member__c,Account__c from Commission__c where Account__c in :sfdcacids];  
        //System.debug('****'+comilst);        
        
        Map<Id,List<Commission__c>> cmmap = new Map<Id,List<Commission__c>>();
        for(Commission__c comi : comilst) {
            salesteamset.add(comi.Sales_Team_Member__c);
            if(cmmap.get(comi.Sales_Team_Member__c)==null) {
                List<Commission__c> cmlst = new List<Commission__c>();
                cmlst.add(comi);
                cmmap.put(comi.Sales_Team_Member__c,cmlst);
            }
            else {
                cmmap.get(comi.Sales_Team_Member__c).add(comi);
            }
        }
        //System.debug('dd11'+cmmap);
        List<User_Budget__c> ownerlst = [select OwnerId,Actual_Dirty__c,Channel__c,Month_Year__c from User_Budget__c where OwnerId in :salesteamset];
        for(User_Budget__c usbdt : ownerlst) {
        //system.debug('###'+thdmap.get(cmmap.get(usbdt.OwnerId).Account__c));
            for(Commission__c c : cmmap.get(usbdt.OwnerId)) {
            for(THD_Sales__c t : thdmap.get(c.Account__c)){
                if((t.POS_Order_Type__c == usbdt.Channel__c) &&
                t.Month_Year__c.year() == usbdt.Month_Year__c.year() &&
                t.Month_Year__c.month() == usbdt.Month_Year__c.month()) {
                    //usbdt.Channel__c = thdmap.get(cmmap.get(usbdt.OwnerId).Account__c)
                    User_Budget__c ub = new User_Budget__c();
                    usbdt.Actual_Dirty__c = True;
                    ub = usbdt;        
                    usbdtmap.put(ub.id,ub);
                }
            } 
            }           
        }    
        update usbdtmap.values();
        }
}

 

How can I improve code coverage for my below class?

Lines which are in red are not covered,

 

Please, help me

 

 

global class My_InsertActual_Batch implements Database.Batchable<sObject> {
    
    Public String Query;
    List<User_Budget__c> ublst = new List<User_Budget__c>();
    List<User_Budget__c> ublst1 = new List<User_Budget__c>();
    List<Commission__c> comlst = new List<Commission__c>();
    List<Commission__c> comlst1 = new List<Commission__c>();
    Set<Id> ubid = new Set<Id>();
    Set<Id> comid = new Set<Id>();
    global Database.queryLocator start(Database.BatchableContext bc) {
        Query = 'SELECT Actual_Dirty__c,Actual__c,Channel__c,Month_Year__c,OwnerId FROM User_Budget__c WHERE Actual_Dirty__c = True LIMIT 20';
        return Database.getQueryLocator(Query);
    }
    
    global void execute(Database.BatchableContext bc, LIST<SObject> lst) {
        for(sObject s : lst) {
           User_Budget__c ub = (User_Budget__c)s; 
           ubid.add(ub.OwnerId);
           ublst.add(ub);
        }
        System.Debug('$$$$$$'+ubid);
        comlst = [select id,Sales_Team_Member__c,
        Total_My_SO_Sales_January__c, Total_My_SO_Sales_February__c,Total_My_SO_Sales_March__c,
        Total_My_SO_Sales_April__c,Total_My_SO_Sales_May__c,Total_My_SO_Sales_June__c,
        Total_My_SO_Sales_July__c,Total_My_SO_Sales_August__c,Total_My_SO_Sales_September__c,
        Total_My_SO_Sales_October__c,Total_My_SO_Sales_November__c,Total_My_SO_Sales_December__c,
        Total_Cash_Carry_Sales_January__c, Total_Cash_Carry_Sales_February__c,Total_Cash_Carry_Sales_March__c,
        Total_Cash_Carry_Sales_April__c,Total_Cash_Carry_Sales_May__c,Total_Cash_Carry_Sales_June__c,
        Total_Cash_Carry_Sales_July__c,Total_Cash_Carry_Sales_August__c,Total_Cash_Carry_Sales_September__c,
        Total_Cash_Carry_Sales_October__c,Total_Cash_Carry_Sales_November__c,Total_Cash_Carry_Sales_December__c 
        from Commission__c where Sales_Team_Member__c in :ubid];
        System.debug('@@@@@'+comlst);
        for(Commission__c com : comlst) {
            comid.add(com.id);
        }
        //ublst = [SELECT Actual_Dirty__c,Actual__c,Channel__c,Month_Year__c,OwnerId FROM User_Budget__c];
        System.debug('******'+ublst);
        for(User_Budget__c u: ublst) {               
           u.Actual__c = 0;
           for(Commission__c com : comlst) {               
               //SO Sales January
               if((u.OwnerId == com.Sales_Team_Member__c) &&
               (u.Actual_Dirty__c == True)) { 
                //u.Actual__c = 0;                   
                   if(u.Channel__c == 'Special Order') {
                       If(u.Month_Year__c.month() == 1) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_My_SO_Sales_January__c;   
                       }                  
                       //SO Sales February
                       if(u.Month_Year__c.month() == 2) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_My_SO_Sales_February__c;                                
                       }     
                       //SO Sales March
                       if(u.Month_Year__c.month() == 3) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_My_SO_Sales_March__c;                                
                       }     
                       //SO Sales April
                       if(u.Month_Year__c.month() == 4) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_My_SO_Sales_April__c;                                
                       }     
                       //SO Sales May
                       if(u.Month_Year__c.month() == 5) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_My_SO_Sales_May__c;                                
                       }     
                       //SO Sales June
                       if(u.Month_Year__c.month() == 6) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_My_SO_Sales_June__c;                                
                       }     
                       //SO Sales July
                       if(u.Month_Year__c.month() == 7) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_My_SO_Sales_July__c;                                
                       }     
                       //SO Sales August
                       if(u.Month_Year__c.month() == 8) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_My_SO_Sales_August__c;                                
                       }     
                       //SO Sales September
                       if(u.Month_Year__c.month() == 9) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_My_SO_Sales_September__c;                                
                       }      
                       //SO Sales October
                       if(u.Month_Year__c.month() == 10) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_My_SO_Sales_October__c;    
                            System.debug(com.Total_My_SO_Sales_October__c+'######'+u.Actual__c+u.Month_Year__c.month()); 
                            System.debug(u.Actual_Dirty__c+'!!!!!');             
                       }  
                       //SO Sales November
                       if(u.Month_Year__c.month() == 11) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_My_SO_Sales_November__c;                                
                       }   
                       //SO Sales December
                       if(u.Month_Year__c.month() == 12) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_My_SO_Sales_December__c;                                
                       } 
                   }                
                   //CC Sales January
                   if(u.Channel__c == 'Cash & Carry') {
                       if(u.Month_Year__c.month() == 1) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_Cash_Carry_Sales_January__c; 
                           }                  
                       //CC Sales February
                       if(u.Month_Year__c.month() == 2) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_Cash_Carry_Sales_February__c;                                
                       }     
                       //CC Sales March
                       if(u.Month_Year__c.month() == 3) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_Cash_Carry_Sales_March__c;                                
                       }     
                       //CC Sales April
                       if(u.Month_Year__c.month() == 4) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_Cash_Carry_Sales_April__c;                                
                       }     
                       //CC Sales May
                       if(u.Month_Year__c.month() == 5) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_Cash_Carry_Sales_May__c;                                
                       }     
                       //CC Sales June
                       if(u.Month_Year__c.month() == 6) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_Cash_Carry_Sales_June__c;                                
                       }     
                       //CC Sales July
                       if(u.Month_Year__c.month() == 7) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_Cash_Carry_Sales_July__c;                                
                       }     
                       //CC Sales August
                       if(u.Month_Year__c.month() == 8) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_Cash_Carry_Sales_August__c;                                
                       }     
                       //CC Sales September
                       if(u.Month_Year__c.month() == 9) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_Cash_Carry_Sales_September__c;                                
                       }      
                       //CC Sales October
                       if(u.Month_Year__c.month() == 10) {
                            //u.Actual__c = 0;
                            u.Actual__c += com.Total_Cash_Carry_Sales_October__c;    
                            System.debug(com.Total_Cash_Carry_Sales_October__c+'######'+u.Actual__c+u.Month_Year__c.month()); 
                            System.debug(u.Actual_Dirty__c+'!!!!!');             
                       }  
                       //CC Sales November
                       if(u.Month_Year__c.month() == 11) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_Cash_Carry_Sales_November__c;                                
                       }   
                       //CC Sales December
                       if(u.Month_Year__c.month() == 12) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_Cash_Carry_Sales_December
__c;                                
                       } 
                   }
               }                  
           } 
           u.Actual_Dirty__c = False; 
           ublst1.add(u);
        }
        update ublst1;        
    }
    
    global void finish(Database.BatchableContext bc) {
         AsyncApexJob a = [SELECT Id, Status, NumberOfErrors, JobItemsProcessed,
         TotalJobItems, CreatedBy.Email
         FROM AsyncApexJob WHERE Id =:BC.getJobId()];
         If(a.Status == 'Completed') {
             My_scheduledActual_Batch bs = new My_scheduledActual_Batch();
             Datetime sysTime = System.now();
             sysTime = sysTime.addminutes(1);
             //String tStr = '0 05 * * * ? '; 
             String chron_exp = '' + sysTime.second() + ' ' + sysTime.minute() + ' ' +
             sysTime.hour() + ' ' + sysTime.day() + ' ' + sysTime.month() + ' ? ' + sysTime.year();            
             System.schedule('Update Actual'+sysTime.getTime(),chron_exp, bs);
             
         }         
         If(a.Status != 'Completed') {
              // Send an email to the Apex job's submitter notifying of job completion. 
    
               Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
               String[] toAddresses = new String[] {a.CreatedBy.Email,’mail@mail.com'};
               mail.setToAddresses(toAddresses);
               mail.setSubject('Apex Sharing Recalculation ' + a.Status);
               mail.setPlainTextBody
               ('The batch Apex job processed ' + a.TotalJobItems +
               ' batches with '+ a.NumberOfErrors + ' failures.');
               Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
               
               My_scheduledActual_Batch bs = new My_scheduledActual_Batch();
               Datetime sysTime = System.now();
               sysTime = sysTime.addminutes(6);
               //String tStr = '0 05 * * * ? '; 
               String chron_exp = '' + sysTime.second() + ' ' + sysTime.minute() + ' ' +
               sysTime.hour() + ' ' + sysTime.day() + ' ' + sysTime.month() + ' ? ' + sysTime.year();            
               System.schedule('Update Actual'+sysTime.getTime(),chron_exp, bs);

         }
    }
}

 

 

 

Below is my test calss:

 

@isTest
private class Test_My_InsertActual_Batch {
    static testMethod void My_InsertActual_Batch(){
        
        User u1 = [SELECT Id,Name FROM User WHERE 
        Email = 'mail@mail.com' limit 1];  
        List <User_Budget__c> ublst = new List<User_Budget__c>();
        
        for(integer i = 0; i<200; i++) {
            Date sysDate = Date.parse('1/31/2012');
            sysDate = sysDate.adddays(i);
            User_Budget__c ub = new User_Budget__c(
            Channel__c = 'Cash & Carry',
            Actual__c = 0,
            Actual_Dirty__c = True,
            Budget_Currency__c = 500,
            Month_Year__c = sysDate,
            OwnerId = u1.id);
            ublst.add(ub);
        }
        insert ublst;
        
        Test.startTest();
            My_InsertActual_Batch insact = new My_InsertActual_Batch();
            ID batchprocessid = Database.executeBatch(insact);
            My_scheduledActual_Batch bs = new My_scheduledActual_Batch();
            Datetime sysTime = System.now();
            sysTime = sysTime.addminutes(1);              
            String chron_exp = '' + sysTime.second() + ' ' + sysTime.minute() + ' ' +
            sysTime.hour() + ' ' + sysTime.day() + ' ' + sysTime.month() + ' ? ' + sysTime.year();            
            System.schedule('Update Actual'+sysTime.getTime(),chron_exp, bs);
        Test.stopTest();
    }
}

 

 

Please, help me to get more coverage for my test class

 

Hi, below is my trigger how can I achieve 75% code coverage for my trigger,

Please, help me to get 75% code coverage.

@isTest
private class Test_My_MonthsRollUp_US{
    static testmethod void test_MonthsRollup_Us(){
        Profile p=[SELECT Id FROM profile WHERE name='System Administrator'];
        UserRole ur = new UserRole(Name='Role1'); 
        User u=new User(UserRoleid=ur.id,alias = 'user123', 
        email='user123@mail.com',emailencodingkey='UTF-8', lastname='u',
        languagelocalekey='en_US',localesidkey='en_US',
        profileid = p.Id,timezonesidkey='America/Los_Angeles',username='user@mail.com',CommunityNickname='usr');
        insert u;       
        RecordType rt = [select id, name 
                     from Recordtype 
                     where sobjectType ='Account' and name = 'Standard Account'];
        Account acc = new Account(
         RecordTypeid = rt.id ,
         Name = 'abc' ,
         Estimated_Annual_Spend__c = 1200 ,
         Market_Segment__c = 'Exporter' ,
         Lead_Source__c = 'ARCAT' ,   
         BillingStreet ='24,marg' ,
         BillingCity = 'kolkata' ,
         BillingState = 'WB' ,
         BillingPostalCode = '700061' ,
         BillingCountry ='India' ,
         Tier_Level__c = 'Tier 1',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);         
         insert acc;
        
        List<THD_Sales__c> thdlst = new List<THD_Sales__c>();
        THD_Sales__c thdrec1 = new THD_Sales__c(
        Month_Year__c = date.parse('1/1/2012'),
        POS_Order_Type__c = 'Special Order',
        Amount__c = 10000.00,
        SFDC_Account_ID__c = acc.id
        ); 
        THD_Sales__c thdrec2 = new THD_Sales__c(
        Month_Year__c = date.parse('2/1/2012'),
        POS_Order_Type__c = 'Special Order',
        Amount__c = 5000.00,
        SFDC_Account_ID__c = acc.id
        );  
        THD_Sales__c thdrec3 = new THD_Sales__c(
        Month_Year__c = date.parse('3/1/2012'),
        POS_Order_Type__c = 'Special Order',
        Amount__c = 4000.00,
        SFDC_Account_ID__c = acc.id
        ); 
        THD_Sales__c thdrec4 = new THD_Sales__c(
        Month_Year__c = date.parse('4/1/2012'),
        POS_Order_Type__c = 'Cash & Carry',
        Amount__c = 3000.00,
        SFDC_Account_ID__c = acc.id
        ); 
        THD_Sales__c thdrec5 = new THD_Sales__c(
        Month_Year__c = date.parse('5/12/2012'),
        POS_Order_Type__c = 'Cash & Carry',
        Amount__c = 2000.00,
        SFDC_Account_ID__c = acc.id
        ); 
        THD_Sales__c thdrec6 = new THD_Sales__c(
        Month_Year__c = date.parse('6/1/2012'),
        POS_Order_Type__c = 'Cash & Carry',
        Amount__c = 1000.00,
        SFDC_Account_ID__c = acc.id
        ); 
        thdlst.add(thdrec1);
        thdlst.add(thdrec2);
        thdlst.add(thdrec3);
        thdlst.add(thdrec4);
        thdlst.add(thdrec5);
        thdlst.add(thdrec6);
        insert thdlst;
        
        
        thdrec1.POS_Order_Type__c = 'Special Order';
        thdrec1.Amount__c = 5000.00;   
       
        thdrec2.POS_Order_Type__c = 'Cash & Carry';
        thdrec2.Amount__c = 8000.00; 
        
        update thdrec1;      
        update thdrec2;
    }
}

 Below is my trigger:

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());
}

 

I am getting below error:

"System.LimitException: Too many SOQL queries: 101"

 

How can I optimize my code to avoid this exception?

Please, help me.

trigger My_AddCommissions_US  on Account (after insert,after update) {

    List<Commission__c> insertCommission=new List<Commission__c>();
    List<Commission__c> updateCommission=new List<Commission__c>();
    set<ID> setupdate=new Set<ID>();
    map<Id,ID> accownermap=new map<id,id>();
    List<Commission__c> lstcom=new List<Commission__c>();
    set<Id> Ownerset=new Set<Id>();   
    ID role;
    map<Id,User> usermap=new Map<Id,User>([select id ,Userroleid,ProfileId from user where IsActive=true]);        
    //Start
   
    //End
    Userrole adminrole1=[select id from Userrole where Name='System Administrator'];
    //system.debug('OOOO'+adminrole1.id);
    Userrole chinarole=[select id from Userrole where Name='China Users'];
    Userrole ITDEVrole=[select id from Userrole where Name='IT DEV'];
    Userrole ProMktgadmin=[select id from Userrole where Name='Pro Mktg Administrator'];
    Userrole ProMktguser=[select id from Userrole where Name='Pro Marketing User'];
    Userrole ProSalesSVP=[select id from Userrole where Name='Pro Sales SVP'];
    Userrole RPSM=[select id from Userrole where Name='RPSM'];
    Userrole MyProRep=[select id from Userrole where Name='My Pro Rep'];

    if(trigger.IsInsert){
        for(Account acc:trigger.new){
            role=usermap.get(acc.OwnerId).Userroleid;
            if(role!=RPSM.id && role!=MyProRep.id && role!=adminrole1.id && role!=chinarole.id && role!=ITDEVrole.id && role!=ProMktgadmin.id && role!=ProMktguser.id && role!=null){
                Commission__c com=new Commission__c();
                com.Account__c=acc.id;               
                com.Sales_Team_Member__c=acc.OwnerId;               
                com.Percent_of_Sale__c=100;
                insertCommission.add(com);
            }
        }
        insert insertCommission;
    }
    if(Trigger.Isupdate){
        for(Account acc:trigger.new){
            if(trigger.newmap.get(acc.id).Ownerid!=trigger.oldmap.get(acc.id).Ownerid){
                setupdate.add(acc.id);
                role=usermap.get(acc.OwnerId).Userroleid;
                if(role!=RPSM.id && role!=MyProRep.id && role!=adminrole1.id && role!=chinarole.id && role!=ITDEVrole.id && role!=ProMktgadmin.id && role!=ProMktguser.id && role!=null){
                    accownermap.put(acc.id,acc.OwnerId);
                    Ownerset.add(trigger.oldmap.get(acc.id).Ownerid);
                }
            }
        }
        lstcom=[select  id,Sales_Team_Member__c,Account__c from Commission__c where Account__c in :setupdate and Sales_Team_Member__c=:Ownerset];          
          
        for(Commission__c c:lstcom){
            c.Sales_Team_Member__c=accownermap.get(c.Account__c);          
            updateCommission.add(c);          
        }
        try{
            update  updateCommission;
        }
        catch(Exception e){
            system.debug('Update failed'+e.getmessage());
        }
    }
}

 

My code should not fire in case of update for below condition

(Trigger.isupdate && Trigger.newmap.get(acccid).Ownerid!=Trigger.oldmap.get(acccid).OwnerId))&& userproId.get(accmap.get(acccid))==p.id)

 

I took all necessary steps, please help me where I have to change.

 

trigger My_InsertAccountTeamMember_US on Account (after insert,after update) {

    List<AccountTeamMember> lstAcTeam = new List<AccountTeamMember>();    
    map<ID,set<ID>> usermap=new map<ID,set<ID>>();   
    map<Id,Id> accmap=new map<Id,Id>();  
    set<ID> setOwner=new set<Id>();
    List<User> userList=new List<User>();    
    set<ID> Parentlst=new set<ID>();
    map<Id,Id> userproId=new Map<Id,Id>();
    set<Id> TeamMember=new Set<Id>();
    List<AccountShare> lstAccShare =new List<AccountShare>();
    List<AccountShare> lstUpdateAccShare = new List<AccountShare>();    
    set<Id> accid=new set<Id>();
    
    Map<Id,UserRole> maprole = new Map<Id,UserRole>([select id,ParentRoleId from UserRole]);
    Profile p=[select Id from Profile where name='Standard User Level 2'];   

    userList=[select id ,Userroleid,ProfileId from user where IsActive=true ];
    for(User u1:userList){
        userproId.put(u1.id,u1.ProfileId);
        if(usermap.get(u1.userroleid)!=null){        
            usermap.get(u1.userroleId).add(u1.id);        
        }
        else{
            set<Id> setuid=new set<ID>();
            setuid.add(u1.id);
            usermap.put(u1.userroleid,setuid);        
        }
    }

    for(Account acc:trigger.new){
        accmap.put(acc.id,acc.ownerid);
        setOwner.add(acc.OwnerId);
        accid.add(acc.id);
    }
    
    map<Id,user> mapownerrole=new map<Id,user>([select id ,userroleid from user where id in:setOwner]);
    Userrole adminrole1=[select id from Userrole where Name='System Administrator'];   
    Userrole chinarole=[select id from Userrole where Name='China Users'];
    Userrole ITDEVrole=[select id from Userrole where Name='IT DEV'];
    Userrole ProMktgadmin=[select id from Userrole where Name='Pro Mktg Administrator'];
    Userrole ProMktguser=[select id from Userrole where Name='Pro Marketing User'];
    Userrole ProSalesSVP=[select id from Userrole where Name='Pro Sales SVP'];   
    
    
    for(Id acccid:accmap.keyset()){
        if((Trigger.isInsert ||(Trigger.isupdate && Trigger.newmap.get(acccid).Ownerid!=Trigger.oldmap.get(acccid).OwnerId))&& userproId.get(accmap.get(acccid))==p.id){             
            id role=mapownerrole.get(accmap.get(acccid)).userroleid;
            Id ParentRoleNext=maprole.get(mapownerrole.get(accmap.get(acccid)).userroleid).ParentRoleId;                      
            if( role!=adminrole1.id && role!=chinarole.id && role!=ITDEVrole.id && role!=ProMktgadmin.id && role!=ProMktguser.id){            
                while(ParentRoleNext!=ProSalesSVP.id){                    
                    if(maprole.containskey(ParentRoleNext))                    
                    Parentlst.add(ParentRoleNext);                   
                    ParentRoleNext=maprole.get(ParentRoleNext).ParentRoleId;
                }                
                for(Id pid: Parentlst){
                    for(id uid1:usermap.get(pid)){                                           
                        lstAcTeam.add(new AccountTeamMember(UserId =uid1,
                        AccountId = acccid,
                        TeamMemberRole = 'Sales Manager'));
                        TeamMember.add(uid1);
                    }          
                }            
            }            
            lstAcTeam.add(new AccountTeamMember(UserId =accmap.get(acccid),
            AccountId = acccid,
            TeamMemberRole = 'Sales Rep'));                        
        }
        Parentlst.clear();
    }
    
    if(lstAcTeam.size()>0){    
        insert lstAcTeam;    
        lstAccShare = [Select Id, AccountId, OpportunityAccessLevel, AccountAccessLevel,ContactAccessLevel, UserOrGroupId ,
                                CaseAccessLevel From AccountShare where AccountId IN: accid
                                  AND UserOrGroupId IN: TeamMember] ;     
    }       

    for (AccountShare ash:lstAccShare ) {
        ash.AccountAccessLevel='Edit';
        ash.ContactAccessLevel='Edit';
        ash.OpportunityAccessLevel='Edit';
        ash.CaseAccessLevel ='Edit';
        lstUpdateAccShare.add(ash);
    }      
    If(lstUpdateAccShare.size()>0) {        
        update lstUpdateAccShare;
    }                       
}

 

 

My code is hittting governor limits incase of multiple records update or insert.

Please help me to reduce number of script statements in this code,

 

Sometimes eventhoug we are not updating anything regarding to account, this trigger is calling,

we have rollup summary fields for this filed from another object is that the reason?

rigger Behr_InsertAccountTeamMember_US on Account (after insert,after update) {
 List<AccountTeamMember> lstAcTeam = new List<AccountTeamMember>();

List<UserRole> lstrole=new List<Userrole>();
map<ID,set<ID>> usermap=new map<ID,set<ID>>();
map<Id,Id> maprole=new map<Id,Id>();
map<Id,Id> accmap=new map<Id,Id>();
map<Id,Id> mapownerrole=new map<Id,Id>();
List<User> ownerrolelst=new List<User>();
set<ID> setOwner=new set<Id>();
List<User> userList=new List<User>();
lstrole=[select id,ParentRoleId from UserRole];
set<ID> Parentlst=new set<ID>();
map<Id,Id> userproId=new Map<Id,Id>();
set<Id> TeamMember=new Set<Id>();
List<AccountShare> lstAccShare =new List<AccountShare>();
List<AccountShare> lstUpdateAccShare = new List<AccountShare>();

set<Id> accid=new set<Id>();

for(UserRole ur:lstrole){
maprole.put(ur.id,ur.ParentRoleId);

}
Profile p=[select Id from Profile where name='Standard User Level 2'];
//Profile p=[select Id from Profile where name='System Administrator'];

userList=[select id ,Userroleid,ProfileId from user where IsActive=true ];
for(User u1:userList){
userproId.put(u1.id,u1.ProfileId);
if(usermap.get(u1.userroleid)!=null){

usermap.get(u1.userroleId).add(u1.id);

}
else{
set<Id> setuid=new set<ID>();
setuid.add(u1.id);
usermap.put(u1.userroleid,setuid);

}
}

for(Account acc:trigger.new){
    accmap.put(acc.id,acc.ownerid);
    setOwner.add(acc.OwnerId);
    accid.add(acc.id);
}

ownerrolelst=[select id ,userroleid from user where id in:setOwner ];
Userrole adminrole1=[select id from Userrole where Name='System Administrator'];
system.debug('OOOO'+adminrole1.id);
Userrole chinarole=[select id from Userrole where Name='China Users'];
Userrole ITDEVrole=[select id from Userrole where Name='IT DEV'];
Userrole ProMktgadmin=[select id from Userrole where Name='Pro Mktg Administrator'];
Userrole ProMktguser=[select id from Userrole where Name='Pro Marketing User'];
 Userrole ProSalesSVP=[select id from Userrole where Name='Pro Sales SVP'];
for(User u:ownerrolelst){
mapownerrole.put(u.id,u.UserRoleId);

}
for(Id acccid:accmap.keyset()){
if((Trigger.isInsert ||(Trigger.isupdate && Trigger.newmap.get(acccid).Ownerid!=Trigger.oldmap.get(acccid).OwnerId))&& userproId.get(accmap.get(acccid))==p.id){
//Parentlst.add(mapownerrole.get(accmap.get(acccid)));
id role=mapownerrole.get(accmap.get(acccid));
Id ParentRoleNext=maprole.get(mapownerrole.get(accmap.get(acccid)));
system.debug('PPPPPPPPPPPPPPPP'+role);

if( role!=adminrole1.id && role!=chinarole.id && role!=ITDEVrole.id && role!=ProMktgadmin.id && role!=ProMktguser.id){

while(ParentRoleNext!=ProSalesSVP.id    ){
system.debug('within if block');
if(maprole.containskey(ParentRoleNext))
system.debug('PPPPPPPPPPPPPPPP'+role);
Parentlst.add(ParentRoleNext);
system.debug('IIIIIIII'+Parentlst);
ParentRoleNext=maprole.get(ParentRoleNext);
}
system.debug('@@@@@@'+Parentlst);
for(Id pid: Parentlst){
for(id uid1:usermap.get(pid)){
AccountTeamMember tm = new AccountTeamMember();
                tm.UserId =uid1;
                tm.AccountId = acccid;
                tm.TeamMemberRole = 'Sales Manager';

lstAcTeam.add(tm);
TeamMember.add(uid1);
}

}

}
AccountTeamMember tm1 = new AccountTeamMember();
                tm1.UserId =accmap.get(acccid);
                tm1.AccountId = acccid;
                tm1.TeamMemberRole = 'Sales Rep';
                lstAcTeam.add(tm1);
  // TeamMember.add(accmap.get(acccid));             
}
}
if(lstAcTeam.size()>0){
insert lstAcTeam;


 lstAccShare = [Select Id, AccountId, OpportunityAccessLevel, AccountAccessLevel,ContactAccessLevel, UserOrGroupId ,
                        CaseAccessLevel From AccountShare where AccountId IN: accid
                          AND UserOrGroupId IN: TeamMember] ;


         }       

 for (AccountShare ash:lstAccShare )
            {
            ash.AccountAccessLevel='Edit';
            ash.ContactAccessLevel='Edit';
            ash.OpportunityAccessLevel='Edit';
            ash.CaseAccessLevel ='Edit';
            
            lstUpdateAccShare.add(ash);
            } 
         update lstUpdateAccShare;

                        

}

 

My code is hitting governor limits, please help me how can I limit the number of statements,

Mainly while importing data using dataloader for upto 5000,  I am getting this error,

please help me to reduce the code 

 

 

trigger Behr_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>();
    //Map<Id,THD_Sales__c> tmap = new Map<Id,THD_Sales__c>([select Id,SFDC_Account_ID__c from THD_Sales__c where id in : Trigger.newmap.keyset()]);
  // If we are inserting, updating, or undeleting, use the new ID values
  if(Trigger.isInsert || Trigger.isUpdate || Trigger.isUndelete)    
    //Set<Id> mset = new Set<Id>();
    for(THD_Sales__c thdsales:Trigger.new)
      updateAccountIds.add(thdsales.SFDC_Account_ID__c);
  // If we are updating, some accounts might change, so include that as well as deletes
  if(Trigger.isUpdate || Trigger.isDelete)
    for(THD_Sales__c thdsales:Trigger.old)
      updateAccountIds.add(thdsales.SFDC_Account_ID__c);
  // Do not create a record for null field
  updateAccountIds.remove(null);  
  // Create in-memory copies for all accounts that will be affected
  for(Id accountId:updateAccountIds){
    updateAccounts.put(accountId,new Account(id=accountId,
    Total_Behr_SO_Sales_January__c=0, Total_Behr_SO_Sales_February__c=0,Total_Behr_SO_Sales_March__c=0,
    Total_Behr_SO_Sales_April__c=0,Total_Behr_SO_Sales_May__c=0,Total_Behr_SO_Sales_June__c=0,
    Total_Behr_SO_Sales_July__c=0,Total_Behr_SO_Sales_August__c=0,Total_Behr_SO_Sales_September__c=0,
    Total_Behr_SO_Sales_October__c=0,Total_Behr_SO_Sales_November__c=0,Total_Behr_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
  List<THD_Sales__c> thdlst = [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 :updateAccountIds and Month_Year__c != null];
  for(THD_Sales__c thdsales:thdlst){  
  //T SO January
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Special Order') && 
  thdsales.Month_Year__c.month() == 1) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Behr_SO_Sales_January__c += thdsales.Amount__c;
      System.Debug('nnnnnnnnn'+string.valueof(thdsales.Month_Year__c).substring(5,7));
      System.Debug('ccccccccc'+thdsales.Month_Year__c.month());
  }
  //T SO February
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Special Order') && 
  thdsales.Month_Year__c.month() == 2) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Behr_SO_Sales_February__c += thdsales.Amount__c;      
  }
  //T SO March
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Special Order') && 
  thdsales.Month_Year__c.month() == 3) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Behr_SO_Sales_March__c += thdsales.Amount__c;      
  }
  //T SO April
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Special Order') && 
  thdsales.Month_Year__c.month() == 4) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Behr_SO_Sales_April__c += thdsales.Amount__c;      
  }
  //T SO May
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Special Order') && 
  thdsales.Month_Year__c.month() == 5) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Behr_SO_Sales_May__c += thdsales.Amount__c;      
  }
  //T SO June
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Special Order') && 
  thdsales.Month_Year__c.month() == 6) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Behr_SO_Sales_June__c += thdsales.Amount__c;      
  }
  //T SO July
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Special Order') && 
  thdsales.Month_Year__c.month() == 7) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Behr_SO_Sales_July__c += thdsales.Amount__c;      
  }
  //T SO August
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Special Order') && 
  thdsales.Month_Year__c.month() == 8) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Behr_SO_Sales_August__c += thdsales.Amount__c;      
  }
  //T SO September
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Special Order') && 
  thdsales.Month_Year__c.month() == 9) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Behr_SO_Sales_September__c += thdsales.Amount__c;      
  }
  //T SO October
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Special Order') && 
  thdsales.Month_Year__c.month() == 10) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Behr_SO_Sales_October__c += thdsales.Amount__c;      
  }
  //T SO November
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Special Order') && 
  thdsales.Month_Year__c.month() == 11) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Behr_SO_Sales_November__c += thdsales.Amount__c;      
  }
  //T SO December
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Special Order') && 
  thdsales.Month_Year__c.month() == 12) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Behr_SO_Sales_December__c += thdsales.Amount__c;      
  }
  
  //T CC January
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Cash & Carry') && 
  thdsales.Month_Year__c.month() == 1) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Cash_Carry_Sales_January__c += thdsales.Amount__c;      
  }
  //T CC February
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Cash & Carry') && 
  thdsales.Month_Year__c.month() == 2) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Cash_Carry_Sales_February__c += thdsales.Amount__c;      
  }
  //T CC March
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Cash & Carry') && 
  thdsales.Month_Year__c.month() == 3) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Cash_Carry_Sales_March__c += thdsales.Amount__c;      
  }
  //T CC April
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Cash & Carry') && 
  thdsales.Month_Year__c.month() == 4) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Cash_Carry_Sales_April__c += thdsales.Amount__c;      
  }
  //T CC May
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Cash & Carry') && 
  thdsales.Month_Year__c.month() == 5) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Cash_Carry_Sales_May__c += thdsales.Amount__c;      
  }
  //T CC June
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Cash & Carry') && 
  thdsales.Month_Year__c.month() == 6) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Cash_Carry_Sales_June__c += thdsales.Amount__c;      
  }
  //T CC July
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Cash & Carry') && 
  thdsales.Month_Year__c.month() == 7) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Cash_Carry_Sales_July__c += thdsales.Amount__c;      
  }
  //T CC August
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Cash & Carry') && 
  thdsales.Month_Year__c.month() == 8) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Cash_Carry_Sales_August__c += thdsales.Amount__c;      
  }
  //T CC September
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Cash & Carry') && 
  thdsales.Month_Year__c.month() == 9) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Cash_Carry_Sales_September__c += thdsales.Amount__c;      
  }
  //T CC October
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Cash & Carry') && 
  thdsales.Month_Year__c.month() == 10) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Cash_Carry_Sales_October__c += thdsales.Amount__c;      
  }
  //T CC November
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Cash & Carry') && 
  thdsales.Month_Year__c.month() == 11) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Cash_Carry_Sales_November__c += thdsales.Amount__c;      
  }
  //T CC December
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Cash & Carry') && 
  thdsales.Month_Year__c.month() == 12) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Cash_Carry_Sales_December__c += thdsales.Amount__c;      
  }
    
    }
    }
  // Update all the accounts with new values.
  Database.update(updateAccounts.values());
}

 

I need to create 40 formula fields for an object is there any govarnor limits

trigger Behr_AmountOfMonths_US on THD_Sales__c (after insert,after update) {
    
    Set<Id> accset = new Set<Id>();
    Set<Id> thdset = new Set<Id>();
    List<THD_Sales__c> thdlst = new List<THD_Sales__c>();
    list<account> acclist = new list<account>();
    Map<Id,THD_Sales__c> thdmap = new Map<Id,THD_Sales__c>();
   map<Id,Double> AccountMap = new map <Id,Double>();

    for(THD_Sales__c thd : Trigger.new) {
        if(thd.POS_Order_Type__c == 'Special Order' && MONTH(thd.Month_Year__c)=1) {
            accset.add(thd.SFDC_Account_ID__c);            
        }
    }
    
    for(AggregateResult q:[SELECT SFDC_Account_ID__c sfdcAccId,sum(Amount__c)tot FROM THD_Sales__c 
    where SFDC_Account_ID__c in :accset group by SFDC_Account_ID__c]) {
        //AccountMap.put((Id)q.get('SFDC_Account_ID__c'),(Double)q.get('expr0'));
        Id sfdcId = (Id) q.get('sfdcAccId');
        Double dTotal = (decimal) q.get('tot'); 
        AccountMap.put(sfdcId, dTotal);
    } 
}

 Error: Compile Error: Method does not exist or incorrect signature: MONTH(Date) at line 11 column 56

if(thd.POS_Order_Type__c == 'Special Order' && MONTH(thd.Month_Year__c)=1)

trigger Behr_AmountOfMonths_US on THD_Sales__c (after insert,after update) {
    
    Set<Id> accset = new Set<Id>();
    Set<Id> thdset = new Set<Id>();
    List<THD_Sales__c> thdlst = new List<THD_Sales__c>();
    Map<Id,THD_Sales__c> thdmap = new Map<Id,THD_Sales__c>();
   map<Id,Double> AccountMap = new map <Id,Double>();

    for(THD_Sales__c thd : Trigger.new) {
        if(thd.POS_Order_Type__c == 'Special Order') {
            accset.add(thd.SFDC_Account_ID__c);            
        }
    }
    for(AggregateResult q:[SELECT SFDC_Account_ID__c,sum(Amount__c)tot FROM THD_Sales__c where SFDC_Account_ID__c in :accset group by SFDC_Account_ID__c]) {
        AccountMap.put((Id)q.get('SFDC_Account_ID__c'),(Double)q.get('expr0'));
    }    
}

  Error: Compile Error: line 15:69 no viable alternative at character '' at line 15 column 69

 

AccountMap.put((Id)q.get('SFDC_Account_ID__c'),(Double)q.get('expr0'));

I am displaying all fields of other sObjects also in same standard controller, but if I click on save it is saving data into current controller object? Please help how to write logic

 

Parent Object:

Account: sum_of_sale (which contains sum of all the percent-of-sale)

Child Object:

Commisstion: Percent_of_sale (which contains precentage of all commissions)

 

Validation rule: while editing I am getting error

i was bulk testing this trigger and while it works in the ui i get this error:

 

"setParentFieldsFromChildTriggeronProgram: execution of BeforeInsert

caused by: System.ListException: Duplicate id in list: 006G000000JvcxjIAB

Trigger.setParentFieldsFromChildTriggeronProgram: line 27, column 1"

 

Here's the trigger.  

trigger setParentFieldsFromChildTriggeronProgram on Program__c (before insert, before update) 
{              
     List<Opportunity> oppList = new List<Opportunity>();
     Set<id> Ids = new Set<id>();
     for (Program__c prgm : Trigger.new) {
         Ids.add(prgm.Opportunity__c);
        }
     
     Map<id,Opportunity> oppMap = new Map<id,Opportunity>([Select Id,Product_Group__c,Product_Area__c,Product_Family__c,Product_Family_Child__c from Opportunity Where Id in :Ids]);  
     
     for (Program__c prgm : Trigger.new) 
     {
                 Opportunity o = oppMap.get(prgm.Opportunity__c);
                 o.Product_Group__c = prgm.Product_Group__c;
                 o.Product_Area__c = prgm.Product_Area__c;
                 o.Product_Family__c = prgm.Product_Family__c;
                 o.Product_Family_Child__c = prgm.Product_Family_Child__c;
                 oppList.add(o);
     }
                 update oppList;
     
}

 

Please help me how   can I improve my code.

 

My trigger is working when I tested it through user interface,

 

I tested my trigger by inserting 10 records using dataloader.

 

But, when I was inserting one lakh records with the batch size of 20. It is not working incase of insert.

but it is working for delete operation

 

My scenario:

 

I have three objects thdsales, commission and userbudget.

 

Userbudget (owner)

Commission (owner, Account number)

Thdsales (Account number)

 

I need to compare (date and channel type) which are common in userbudget and thdsales through commission.

If matches then I need to update a checkbox as true in userbudget.

 

Below is my trigger:  

 

for insert, instead of one month it is marking another month.(suppose instead of aug marking july record)

 

 

trigger MY_markAllCommissions_US on THD_Sales__c (before insert,after update,after delete) {
    
    if(Trigger.isinsert || Trigger.isupdate) {
        List<Commission__c> comilst = new List<Commission__c>();
        Set<Id> salesteamset = new Set<Id>();
        Set<Id> sfdcacids = new Set<Id>();
        Map<Id,User_Budget__c> usbdtmap = new Map<Id,User_Budget__c>();
        Map<Id,List<THD_Sales__c>> thdmap = new Map<Id,List<THD_Sales__c>>();
        for(THD_Sales__c thd : Trigger.new) {        
            sfdcacids.add(thd.SFDC_Account_ID__c);
            If(thdmap.get(thd.SFDC_Account_ID__c)==null) {
                List<THD_Sales__c> thdlst = new List<THD_Sales__c>();
                thdlst.add(thd);
                thdmap.put(thd.SFDC_Account_ID__c,thdlst);
            }
            else {
                thdmap.get(thd.SFDC_Account_ID__c).add(thd);
            }                      
        }
        //system.debug('$$$'+thdmap);
        comilst = [select id,Sales_Team_Member__c,Account__c from Commission__c where Account__c in :sfdcacids];  
        //System.debug('****'+comilst);        
        
        Map<Id,List<Commission__c>> cmmap = new Map<Id,List<Commission__c>>();
        for(Commission__c comi : comilst) {
            salesteamset.add(comi.Sales_Team_Member__c);
            if(cmmap.get(comi.Sales_Team_Member__c)==null) {
                List<Commission__c> cmlst = new List<Commission__c>();
                cmlst.add(comi);
                cmmap.put(comi.Sales_Team_Member__c,cmlst);
            }
            else {
                cmmap.get(comi.Sales_Team_Member__c).add(comi);
            }
        }
        //System.debug('dd11'+cmmap);
        List<User_Budget__c> ownerlst = [select OwnerId,Actual_Dirty__c,Channel__c,Month_Year__c from User_Budget__c where OwnerId in :salesteamset];
        for(User_Budget__c usbdt : ownerlst) {
        //system.debug('###'+thdmap.get(cmmap.get(usbdt.OwnerId).Account__c));
            for(Commission__c c : cmmap.get(usbdt.OwnerId)) {
            for(THD_Sales__c t : thdmap.get(c.Account__c)){
                if((t.POS_Order_Type__c == usbdt.Channel__c) &&
                t.Month_Year__c.year() == usbdt.Month_Year__c.year() &&
                t.Month_Year__c.month() == usbdt.Month_Year__c.month()) {
                    //usbdt.Channel__c = thdmap.get(cmmap.get(usbdt.OwnerId).Account__c)
                    User_Budget__c ub = new User_Budget__c();
                    usbdt.Actual_Dirty__c = True;
                    ub = usbdt;        
                    usbdtmap.put(ub.id,ub);
                }
            } 
            }           
        }    
        update usbdtmap.values();
    }
    
    if(Trigger.isdelete || Trigger.isupdate) {
         List<Commission__c> comilst = new List<Commission__c>();
        Set<Id> salesteamset = new Set<Id>();
        Set<Id> sfdcacids = new Set<Id>();
        Map<Id,User_Budget__c> usbdtmap = new Map<Id,User_Budget__c>();
        Map<Id,List<THD_Sales__c>> thdmap = new Map<Id,List<THD_Sales__c>>();
        for(THD_Sales__c thd : Trigger.old) {        
            sfdcacids.add(thd.SFDC_Account_ID__c);
            If(thdmap.get(thd.SFDC_Account_ID__c)==null) {
                List<THD_Sales__c> thdlst = new List<THD_Sales__c>();
                thdlst.add(thd);
                thdmap.put(thd.SFDC_Account_ID__c,thdlst);
            }
            else {
                thdmap.get(thd.SFDC_Account_ID__c).add(thd);
            }                      
        }
        //system.debug('$$$'+thdmap);
        comilst = [select id,Sales_Team_Member__c,Account__c from Commission__c where Account__c in :sfdcacids];  
        //System.debug('****'+comilst);        
        
        Map<Id,List<Commission__c>> cmmap = new Map<Id,List<Commission__c>>();
        for(Commission__c comi : comilst) {
            salesteamset.add(comi.Sales_Team_Member__c);
            if(cmmap.get(comi.Sales_Team_Member__c)==null) {
                List<Commission__c> cmlst = new List<Commission__c>();
                cmlst.add(comi);
                cmmap.put(comi.Sales_Team_Member__c,cmlst);
            }
            else {
                cmmap.get(comi.Sales_Team_Member__c).add(comi);
            }
        }
        //System.debug('dd11'+cmmap);
        List<User_Budget__c> ownerlst = [select OwnerId,Actual_Dirty__c,Channel__c,Month_Year__c from User_Budget__c where OwnerId in :salesteamset];
        for(User_Budget__c usbdt : ownerlst) {
        //system.debug('###'+thdmap.get(cmmap.get(usbdt.OwnerId).Account__c));
            for(Commission__c c : cmmap.get(usbdt.OwnerId)) {
            for(THD_Sales__c t : thdmap.get(c.Account__c)){
                if((t.POS_Order_Type__c == usbdt.Channel__c) &&
                t.Month_Year__c.year() == usbdt.Month_Year__c.year() &&
                t.Month_Year__c.month() == usbdt.Month_Year__c.month()) {
                    //usbdt.Channel__c = thdmap.get(cmmap.get(usbdt.OwnerId).Account__c)
                    User_Budget__c ub = new User_Budget__c();
                    usbdt.Actual_Dirty__c = True;
                    ub = usbdt;        
                    usbdtmap.put(ub.id,ub);
                }
            } 
            }           
        }    
        update usbdtmap.values();
        }
}

 

How can I improve code coverage for my below class?

Lines which are in red are not covered,

 

Please, help me

 

 

global class My_InsertActual_Batch implements Database.Batchable<sObject> {
    
    Public String Query;
    List<User_Budget__c> ublst = new List<User_Budget__c>();
    List<User_Budget__c> ublst1 = new List<User_Budget__c>();
    List<Commission__c> comlst = new List<Commission__c>();
    List<Commission__c> comlst1 = new List<Commission__c>();
    Set<Id> ubid = new Set<Id>();
    Set<Id> comid = new Set<Id>();
    global Database.queryLocator start(Database.BatchableContext bc) {
        Query = 'SELECT Actual_Dirty__c,Actual__c,Channel__c,Month_Year__c,OwnerId FROM User_Budget__c WHERE Actual_Dirty__c = True LIMIT 20';
        return Database.getQueryLocator(Query);
    }
    
    global void execute(Database.BatchableContext bc, LIST<SObject> lst) {
        for(sObject s : lst) {
           User_Budget__c ub = (User_Budget__c)s; 
           ubid.add(ub.OwnerId);
           ublst.add(ub);
        }
        System.Debug('$$$$$$'+ubid);
        comlst = [select id,Sales_Team_Member__c,
        Total_My_SO_Sales_January__c, Total_My_SO_Sales_February__c,Total_My_SO_Sales_March__c,
        Total_My_SO_Sales_April__c,Total_My_SO_Sales_May__c,Total_My_SO_Sales_June__c,
        Total_My_SO_Sales_July__c,Total_My_SO_Sales_August__c,Total_My_SO_Sales_September__c,
        Total_My_SO_Sales_October__c,Total_My_SO_Sales_November__c,Total_My_SO_Sales_December__c,
        Total_Cash_Carry_Sales_January__c, Total_Cash_Carry_Sales_February__c,Total_Cash_Carry_Sales_March__c,
        Total_Cash_Carry_Sales_April__c,Total_Cash_Carry_Sales_May__c,Total_Cash_Carry_Sales_June__c,
        Total_Cash_Carry_Sales_July__c,Total_Cash_Carry_Sales_August__c,Total_Cash_Carry_Sales_September__c,
        Total_Cash_Carry_Sales_October__c,Total_Cash_Carry_Sales_November__c,Total_Cash_Carry_Sales_December__c 
        from Commission__c where Sales_Team_Member__c in :ubid];
        System.debug('@@@@@'+comlst);
        for(Commission__c com : comlst) {
            comid.add(com.id);
        }
        //ublst = [SELECT Actual_Dirty__c,Actual__c,Channel__c,Month_Year__c,OwnerId FROM User_Budget__c];
        System.debug('******'+ublst);
        for(User_Budget__c u: ublst) {               
           u.Actual__c = 0;
           for(Commission__c com : comlst) {               
               //SO Sales January
               if((u.OwnerId == com.Sales_Team_Member__c) &&
               (u.Actual_Dirty__c == True)) { 
                //u.Actual__c = 0;                   
                   if(u.Channel__c == 'Special Order') {
                       If(u.Month_Year__c.month() == 1) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_My_SO_Sales_January__c;   
                       }                  
                       //SO Sales February
                       if(u.Month_Year__c.month() == 2) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_My_SO_Sales_February__c;                                
                       }     
                       //SO Sales March
                       if(u.Month_Year__c.month() == 3) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_My_SO_Sales_March__c;                                
                       }     
                       //SO Sales April
                       if(u.Month_Year__c.month() == 4) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_My_SO_Sales_April__c;                                
                       }     
                       //SO Sales May
                       if(u.Month_Year__c.month() == 5) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_My_SO_Sales_May__c;                                
                       }     
                       //SO Sales June
                       if(u.Month_Year__c.month() == 6) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_My_SO_Sales_June__c;                                
                       }     
                       //SO Sales July
                       if(u.Month_Year__c.month() == 7) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_My_SO_Sales_July__c;                                
                       }     
                       //SO Sales August
                       if(u.Month_Year__c.month() == 8) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_My_SO_Sales_August__c;                                
                       }     
                       //SO Sales September
                       if(u.Month_Year__c.month() == 9) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_My_SO_Sales_September__c;                                
                       }      
                       //SO Sales October
                       if(u.Month_Year__c.month() == 10) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_My_SO_Sales_October__c;    
                            System.debug(com.Total_My_SO_Sales_October__c+'######'+u.Actual__c+u.Month_Year__c.month()); 
                            System.debug(u.Actual_Dirty__c+'!!!!!');             
                       }  
                       //SO Sales November
                       if(u.Month_Year__c.month() == 11) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_My_SO_Sales_November__c;                                
                       }   
                       //SO Sales December
                       if(u.Month_Year__c.month() == 12) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_My_SO_Sales_December__c;                                
                       } 
                   }                
                   //CC Sales January
                   if(u.Channel__c == 'Cash & Carry') {
                       if(u.Month_Year__c.month() == 1) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_Cash_Carry_Sales_January__c; 
                           }                  
                       //CC Sales February
                       if(u.Month_Year__c.month() == 2) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_Cash_Carry_Sales_February__c;                                
                       }     
                       //CC Sales March
                       if(u.Month_Year__c.month() == 3) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_Cash_Carry_Sales_March__c;                                
                       }     
                       //CC Sales April
                       if(u.Month_Year__c.month() == 4) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_Cash_Carry_Sales_April__c;                                
                       }     
                       //CC Sales May
                       if(u.Month_Year__c.month() == 5) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_Cash_Carry_Sales_May__c;                                
                       }     
                       //CC Sales June
                       if(u.Month_Year__c.month() == 6) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_Cash_Carry_Sales_June__c;                                
                       }     
                       //CC Sales July
                       if(u.Month_Year__c.month() == 7) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_Cash_Carry_Sales_July__c;                                
                       }     
                       //CC Sales August
                       if(u.Month_Year__c.month() == 8) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_Cash_Carry_Sales_August__c;                                
                       }     
                       //CC Sales September
                       if(u.Month_Year__c.month() == 9) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_Cash_Carry_Sales_September__c;                                
                       }      
                       //CC Sales October
                       if(u.Month_Year__c.month() == 10) {
                            //u.Actual__c = 0;
                            u.Actual__c += com.Total_Cash_Carry_Sales_October__c;    
                            System.debug(com.Total_Cash_Carry_Sales_October__c+'######'+u.Actual__c+u.Month_Year__c.month()); 
                            System.debug(u.Actual_Dirty__c+'!!!!!');             
                       }  
                       //CC Sales November
                       if(u.Month_Year__c.month() == 11) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_Cash_Carry_Sales_November__c;                                
                       }   
                       //CC Sales December
                       if(u.Month_Year__c.month() == 12) {
                           //u.Actual__c = 0;
                           u.Actual__c += com.Total_Cash_Carry_Sales_December
__c;                                
                       } 
                   }
               }                  
           } 
           u.Actual_Dirty__c = False; 
           ublst1.add(u);
        }
        update ublst1;        
    }
    
    global void finish(Database.BatchableContext bc) {
         AsyncApexJob a = [SELECT Id, Status, NumberOfErrors, JobItemsProcessed,
         TotalJobItems, CreatedBy.Email
         FROM AsyncApexJob WHERE Id =:BC.getJobId()];
         If(a.Status == 'Completed') {
             My_scheduledActual_Batch bs = new My_scheduledActual_Batch();
             Datetime sysTime = System.now();
             sysTime = sysTime.addminutes(1);
             //String tStr = '0 05 * * * ? '; 
             String chron_exp = '' + sysTime.second() + ' ' + sysTime.minute() + ' ' +
             sysTime.hour() + ' ' + sysTime.day() + ' ' + sysTime.month() + ' ? ' + sysTime.year();            
             System.schedule('Update Actual'+sysTime.getTime(),chron_exp, bs);
             
         }         
         If(a.Status != 'Completed') {
              // Send an email to the Apex job's submitter notifying of job completion. 
    
               Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
               String[] toAddresses = new String[] {a.CreatedBy.Email,’mail@mail.com'};
               mail.setToAddresses(toAddresses);
               mail.setSubject('Apex Sharing Recalculation ' + a.Status);
               mail.setPlainTextBody
               ('The batch Apex job processed ' + a.TotalJobItems +
               ' batches with '+ a.NumberOfErrors + ' failures.');
               Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
               
               My_scheduledActual_Batch bs = new My_scheduledActual_Batch();
               Datetime sysTime = System.now();
               sysTime = sysTime.addminutes(6);
               //String tStr = '0 05 * * * ? '; 
               String chron_exp = '' + sysTime.second() + ' ' + sysTime.minute() + ' ' +
               sysTime.hour() + ' ' + sysTime.day() + ' ' + sysTime.month() + ' ? ' + sysTime.year();            
               System.schedule('Update Actual'+sysTime.getTime(),chron_exp, bs);

         }
    }
}

 

 

 

Below is my test calss:

 

@isTest
private class Test_My_InsertActual_Batch {
    static testMethod void My_InsertActual_Batch(){
        
        User u1 = [SELECT Id,Name FROM User WHERE 
        Email = 'mail@mail.com' limit 1];  
        List <User_Budget__c> ublst = new List<User_Budget__c>();
        
        for(integer i = 0; i<200; i++) {
            Date sysDate = Date.parse('1/31/2012');
            sysDate = sysDate.adddays(i);
            User_Budget__c ub = new User_Budget__c(
            Channel__c = 'Cash & Carry',
            Actual__c = 0,
            Actual_Dirty__c = True,
            Budget_Currency__c = 500,
            Month_Year__c = sysDate,
            OwnerId = u1.id);
            ublst.add(ub);
        }
        insert ublst;
        
        Test.startTest();
            My_InsertActual_Batch insact = new My_InsertActual_Batch();
            ID batchprocessid = Database.executeBatch(insact);
            My_scheduledActual_Batch bs = new My_scheduledActual_Batch();
            Datetime sysTime = System.now();
            sysTime = sysTime.addminutes(1);              
            String chron_exp = '' + sysTime.second() + ' ' + sysTime.minute() + ' ' +
            sysTime.hour() + ' ' + sysTime.day() + ' ' + sysTime.month() + ' ? ' + sysTime.year();            
            System.schedule('Update Actual'+sysTime.getTime(),chron_exp, bs);
        Test.stopTest();
    }
}

 

 

Please, help me to get more coverage for my test class

 

Hi, below is my trigger how can I achieve 75% code coverage for my trigger,

Please, help me to get 75% code coverage.

@isTest
private class Test_My_MonthsRollUp_US{
    static testmethod void test_MonthsRollup_Us(){
        Profile p=[SELECT Id FROM profile WHERE name='System Administrator'];
        UserRole ur = new UserRole(Name='Role1'); 
        User u=new User(UserRoleid=ur.id,alias = 'user123', 
        email='user123@mail.com',emailencodingkey='UTF-8', lastname='u',
        languagelocalekey='en_US',localesidkey='en_US',
        profileid = p.Id,timezonesidkey='America/Los_Angeles',username='user@mail.com',CommunityNickname='usr');
        insert u;       
        RecordType rt = [select id, name 
                     from Recordtype 
                     where sobjectType ='Account' and name = 'Standard Account'];
        Account acc = new Account(
         RecordTypeid = rt.id ,
         Name = 'abc' ,
         Estimated_Annual_Spend__c = 1200 ,
         Market_Segment__c = 'Exporter' ,
         Lead_Source__c = 'ARCAT' ,   
         BillingStreet ='24,marg' ,
         BillingCity = 'kolkata' ,
         BillingState = 'WB' ,
         BillingPostalCode = '700061' ,
         BillingCountry ='India' ,
         Tier_Level__c = 'Tier 1',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);         
         insert acc;
        
        List<THD_Sales__c> thdlst = new List<THD_Sales__c>();
        THD_Sales__c thdrec1 = new THD_Sales__c(
        Month_Year__c = date.parse('1/1/2012'),
        POS_Order_Type__c = 'Special Order',
        Amount__c = 10000.00,
        SFDC_Account_ID__c = acc.id
        ); 
        THD_Sales__c thdrec2 = new THD_Sales__c(
        Month_Year__c = date.parse('2/1/2012'),
        POS_Order_Type__c = 'Special Order',
        Amount__c = 5000.00,
        SFDC_Account_ID__c = acc.id
        );  
        THD_Sales__c thdrec3 = new THD_Sales__c(
        Month_Year__c = date.parse('3/1/2012'),
        POS_Order_Type__c = 'Special Order',
        Amount__c = 4000.00,
        SFDC_Account_ID__c = acc.id
        ); 
        THD_Sales__c thdrec4 = new THD_Sales__c(
        Month_Year__c = date.parse('4/1/2012'),
        POS_Order_Type__c = 'Cash & Carry',
        Amount__c = 3000.00,
        SFDC_Account_ID__c = acc.id
        ); 
        THD_Sales__c thdrec5 = new THD_Sales__c(
        Month_Year__c = date.parse('5/12/2012'),
        POS_Order_Type__c = 'Cash & Carry',
        Amount__c = 2000.00,
        SFDC_Account_ID__c = acc.id
        ); 
        THD_Sales__c thdrec6 = new THD_Sales__c(
        Month_Year__c = date.parse('6/1/2012'),
        POS_Order_Type__c = 'Cash & Carry',
        Amount__c = 1000.00,
        SFDC_Account_ID__c = acc.id
        ); 
        thdlst.add(thdrec1);
        thdlst.add(thdrec2);
        thdlst.add(thdrec3);
        thdlst.add(thdrec4);
        thdlst.add(thdrec5);
        thdlst.add(thdrec6);
        insert thdlst;
        
        
        thdrec1.POS_Order_Type__c = 'Special Order';
        thdrec1.Amount__c = 5000.00;   
       
        thdrec2.POS_Order_Type__c = 'Cash & Carry';
        thdrec2.Amount__c = 8000.00; 
        
        update thdrec1;      
        update thdrec2;
    }
}

 Below is my trigger:

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());
}

 

I am getting below error:

"System.LimitException: Too many SOQL queries: 101"

 

How can I optimize my code to avoid this exception?

Please, help me.

trigger My_AddCommissions_US  on Account (after insert,after update) {

    List<Commission__c> insertCommission=new List<Commission__c>();
    List<Commission__c> updateCommission=new List<Commission__c>();
    set<ID> setupdate=new Set<ID>();
    map<Id,ID> accownermap=new map<id,id>();
    List<Commission__c> lstcom=new List<Commission__c>();
    set<Id> Ownerset=new Set<Id>();   
    ID role;
    map<Id,User> usermap=new Map<Id,User>([select id ,Userroleid,ProfileId from user where IsActive=true]);        
    //Start
   
    //End
    Userrole adminrole1=[select id from Userrole where Name='System Administrator'];
    //system.debug('OOOO'+adminrole1.id);
    Userrole chinarole=[select id from Userrole where Name='China Users'];
    Userrole ITDEVrole=[select id from Userrole where Name='IT DEV'];
    Userrole ProMktgadmin=[select id from Userrole where Name='Pro Mktg Administrator'];
    Userrole ProMktguser=[select id from Userrole where Name='Pro Marketing User'];
    Userrole ProSalesSVP=[select id from Userrole where Name='Pro Sales SVP'];
    Userrole RPSM=[select id from Userrole where Name='RPSM'];
    Userrole MyProRep=[select id from Userrole where Name='My Pro Rep'];

    if(trigger.IsInsert){
        for(Account acc:trigger.new){
            role=usermap.get(acc.OwnerId).Userroleid;
            if(role!=RPSM.id && role!=MyProRep.id && role!=adminrole1.id && role!=chinarole.id && role!=ITDEVrole.id && role!=ProMktgadmin.id && role!=ProMktguser.id && role!=null){
                Commission__c com=new Commission__c();
                com.Account__c=acc.id;               
                com.Sales_Team_Member__c=acc.OwnerId;               
                com.Percent_of_Sale__c=100;
                insertCommission.add(com);
            }
        }
        insert insertCommission;
    }
    if(Trigger.Isupdate){
        for(Account acc:trigger.new){
            if(trigger.newmap.get(acc.id).Ownerid!=trigger.oldmap.get(acc.id).Ownerid){
                setupdate.add(acc.id);
                role=usermap.get(acc.OwnerId).Userroleid;
                if(role!=RPSM.id && role!=MyProRep.id && role!=adminrole1.id && role!=chinarole.id && role!=ITDEVrole.id && role!=ProMktgadmin.id && role!=ProMktguser.id && role!=null){
                    accownermap.put(acc.id,acc.OwnerId);
                    Ownerset.add(trigger.oldmap.get(acc.id).Ownerid);
                }
            }
        }
        lstcom=[select  id,Sales_Team_Member__c,Account__c from Commission__c where Account__c in :setupdate and Sales_Team_Member__c=:Ownerset];          
          
        for(Commission__c c:lstcom){
            c.Sales_Team_Member__c=accownermap.get(c.Account__c);          
            updateCommission.add(c);          
        }
        try{
            update  updateCommission;
        }
        catch(Exception e){
            system.debug('Update failed'+e.getmessage());
        }
    }
}

 

My code is hittting governor limits incase of multiple records update or insert.

Please help me to reduce number of script statements in this code,

 

Sometimes eventhoug we are not updating anything regarding to account, this trigger is calling,

we have rollup summary fields for this filed from another object is that the reason?

rigger Behr_InsertAccountTeamMember_US on Account (after insert,after update) {
 List<AccountTeamMember> lstAcTeam = new List<AccountTeamMember>();

List<UserRole> lstrole=new List<Userrole>();
map<ID,set<ID>> usermap=new map<ID,set<ID>>();
map<Id,Id> maprole=new map<Id,Id>();
map<Id,Id> accmap=new map<Id,Id>();
map<Id,Id> mapownerrole=new map<Id,Id>();
List<User> ownerrolelst=new List<User>();
set<ID> setOwner=new set<Id>();
List<User> userList=new List<User>();
lstrole=[select id,ParentRoleId from UserRole];
set<ID> Parentlst=new set<ID>();
map<Id,Id> userproId=new Map<Id,Id>();
set<Id> TeamMember=new Set<Id>();
List<AccountShare> lstAccShare =new List<AccountShare>();
List<AccountShare> lstUpdateAccShare = new List<AccountShare>();

set<Id> accid=new set<Id>();

for(UserRole ur:lstrole){
maprole.put(ur.id,ur.ParentRoleId);

}
Profile p=[select Id from Profile where name='Standard User Level 2'];
//Profile p=[select Id from Profile where name='System Administrator'];

userList=[select id ,Userroleid,ProfileId from user where IsActive=true ];
for(User u1:userList){
userproId.put(u1.id,u1.ProfileId);
if(usermap.get(u1.userroleid)!=null){

usermap.get(u1.userroleId).add(u1.id);

}
else{
set<Id> setuid=new set<ID>();
setuid.add(u1.id);
usermap.put(u1.userroleid,setuid);

}
}

for(Account acc:trigger.new){
    accmap.put(acc.id,acc.ownerid);
    setOwner.add(acc.OwnerId);
    accid.add(acc.id);
}

ownerrolelst=[select id ,userroleid from user where id in:setOwner ];
Userrole adminrole1=[select id from Userrole where Name='System Administrator'];
system.debug('OOOO'+adminrole1.id);
Userrole chinarole=[select id from Userrole where Name='China Users'];
Userrole ITDEVrole=[select id from Userrole where Name='IT DEV'];
Userrole ProMktgadmin=[select id from Userrole where Name='Pro Mktg Administrator'];
Userrole ProMktguser=[select id from Userrole where Name='Pro Marketing User'];
 Userrole ProSalesSVP=[select id from Userrole where Name='Pro Sales SVP'];
for(User u:ownerrolelst){
mapownerrole.put(u.id,u.UserRoleId);

}
for(Id acccid:accmap.keyset()){
if((Trigger.isInsert ||(Trigger.isupdate && Trigger.newmap.get(acccid).Ownerid!=Trigger.oldmap.get(acccid).OwnerId))&& userproId.get(accmap.get(acccid))==p.id){
//Parentlst.add(mapownerrole.get(accmap.get(acccid)));
id role=mapownerrole.get(accmap.get(acccid));
Id ParentRoleNext=maprole.get(mapownerrole.get(accmap.get(acccid)));
system.debug('PPPPPPPPPPPPPPPP'+role);

if( role!=adminrole1.id && role!=chinarole.id && role!=ITDEVrole.id && role!=ProMktgadmin.id && role!=ProMktguser.id){

while(ParentRoleNext!=ProSalesSVP.id    ){
system.debug('within if block');
if(maprole.containskey(ParentRoleNext))
system.debug('PPPPPPPPPPPPPPPP'+role);
Parentlst.add(ParentRoleNext);
system.debug('IIIIIIII'+Parentlst);
ParentRoleNext=maprole.get(ParentRoleNext);
}
system.debug('@@@@@@'+Parentlst);
for(Id pid: Parentlst){
for(id uid1:usermap.get(pid)){
AccountTeamMember tm = new AccountTeamMember();
                tm.UserId =uid1;
                tm.AccountId = acccid;
                tm.TeamMemberRole = 'Sales Manager';

lstAcTeam.add(tm);
TeamMember.add(uid1);
}

}

}
AccountTeamMember tm1 = new AccountTeamMember();
                tm1.UserId =accmap.get(acccid);
                tm1.AccountId = acccid;
                tm1.TeamMemberRole = 'Sales Rep';
                lstAcTeam.add(tm1);
  // TeamMember.add(accmap.get(acccid));             
}
}
if(lstAcTeam.size()>0){
insert lstAcTeam;


 lstAccShare = [Select Id, AccountId, OpportunityAccessLevel, AccountAccessLevel,ContactAccessLevel, UserOrGroupId ,
                        CaseAccessLevel From AccountShare where AccountId IN: accid
                          AND UserOrGroupId IN: TeamMember] ;


         }       

 for (AccountShare ash:lstAccShare )
            {
            ash.AccountAccessLevel='Edit';
            ash.ContactAccessLevel='Edit';
            ash.OpportunityAccessLevel='Edit';
            ash.CaseAccessLevel ='Edit';
            
            lstUpdateAccShare.add(ash);
            } 
         update lstUpdateAccShare;

                        

}

 

My code is hitting governor limits, please help me how can I limit the number of statements,

Mainly while importing data using dataloader for upto 5000,  I am getting this error,

please help me to reduce the code 

 

 

trigger Behr_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>();
    //Map<Id,THD_Sales__c> tmap = new Map<Id,THD_Sales__c>([select Id,SFDC_Account_ID__c from THD_Sales__c where id in : Trigger.newmap.keyset()]);
  // If we are inserting, updating, or undeleting, use the new ID values
  if(Trigger.isInsert || Trigger.isUpdate || Trigger.isUndelete)    
    //Set<Id> mset = new Set<Id>();
    for(THD_Sales__c thdsales:Trigger.new)
      updateAccountIds.add(thdsales.SFDC_Account_ID__c);
  // If we are updating, some accounts might change, so include that as well as deletes
  if(Trigger.isUpdate || Trigger.isDelete)
    for(THD_Sales__c thdsales:Trigger.old)
      updateAccountIds.add(thdsales.SFDC_Account_ID__c);
  // Do not create a record for null field
  updateAccountIds.remove(null);  
  // Create in-memory copies for all accounts that will be affected
  for(Id accountId:updateAccountIds){
    updateAccounts.put(accountId,new Account(id=accountId,
    Total_Behr_SO_Sales_January__c=0, Total_Behr_SO_Sales_February__c=0,Total_Behr_SO_Sales_March__c=0,
    Total_Behr_SO_Sales_April__c=0,Total_Behr_SO_Sales_May__c=0,Total_Behr_SO_Sales_June__c=0,
    Total_Behr_SO_Sales_July__c=0,Total_Behr_SO_Sales_August__c=0,Total_Behr_SO_Sales_September__c=0,
    Total_Behr_SO_Sales_October__c=0,Total_Behr_SO_Sales_November__c=0,Total_Behr_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
  List<THD_Sales__c> thdlst = [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 :updateAccountIds and Month_Year__c != null];
  for(THD_Sales__c thdsales:thdlst){  
  //T SO January
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Special Order') && 
  thdsales.Month_Year__c.month() == 1) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Behr_SO_Sales_January__c += thdsales.Amount__c;
      System.Debug('nnnnnnnnn'+string.valueof(thdsales.Month_Year__c).substring(5,7));
      System.Debug('ccccccccc'+thdsales.Month_Year__c.month());
  }
  //T SO February
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Special Order') && 
  thdsales.Month_Year__c.month() == 2) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Behr_SO_Sales_February__c += thdsales.Amount__c;      
  }
  //T SO March
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Special Order') && 
  thdsales.Month_Year__c.month() == 3) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Behr_SO_Sales_March__c += thdsales.Amount__c;      
  }
  //T SO April
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Special Order') && 
  thdsales.Month_Year__c.month() == 4) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Behr_SO_Sales_April__c += thdsales.Amount__c;      
  }
  //T SO May
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Special Order') && 
  thdsales.Month_Year__c.month() == 5) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Behr_SO_Sales_May__c += thdsales.Amount__c;      
  }
  //T SO June
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Special Order') && 
  thdsales.Month_Year__c.month() == 6) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Behr_SO_Sales_June__c += thdsales.Amount__c;      
  }
  //T SO July
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Special Order') && 
  thdsales.Month_Year__c.month() == 7) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Behr_SO_Sales_July__c += thdsales.Amount__c;      
  }
  //T SO August
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Special Order') && 
  thdsales.Month_Year__c.month() == 8) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Behr_SO_Sales_August__c += thdsales.Amount__c;      
  }
  //T SO September
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Special Order') && 
  thdsales.Month_Year__c.month() == 9) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Behr_SO_Sales_September__c += thdsales.Amount__c;      
  }
  //T SO October
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Special Order') && 
  thdsales.Month_Year__c.month() == 10) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Behr_SO_Sales_October__c += thdsales.Amount__c;      
  }
  //T SO November
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Special Order') && 
  thdsales.Month_Year__c.month() == 11) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Behr_SO_Sales_November__c += thdsales.Amount__c;      
  }
  //T SO December
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Special Order') && 
  thdsales.Month_Year__c.month() == 12) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Behr_SO_Sales_December__c += thdsales.Amount__c;      
  }
  
  //T CC January
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Cash & Carry') && 
  thdsales.Month_Year__c.month() == 1) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Cash_Carry_Sales_January__c += thdsales.Amount__c;      
  }
  //T CC February
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Cash & Carry') && 
  thdsales.Month_Year__c.month() == 2) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Cash_Carry_Sales_February__c += thdsales.Amount__c;      
  }
  //T CC March
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Cash & Carry') && 
  thdsales.Month_Year__c.month() == 3) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Cash_Carry_Sales_March__c += thdsales.Amount__c;      
  }
  //T CC April
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Cash & Carry') && 
  thdsales.Month_Year__c.month() == 4) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Cash_Carry_Sales_April__c += thdsales.Amount__c;      
  }
  //T CC May
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Cash & Carry') && 
  thdsales.Month_Year__c.month() == 5) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Cash_Carry_Sales_May__c += thdsales.Amount__c;      
  }
  //T CC June
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Cash & Carry') && 
  thdsales.Month_Year__c.month() == 6) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Cash_Carry_Sales_June__c += thdsales.Amount__c;      
  }
  //T CC July
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Cash & Carry') && 
  thdsales.Month_Year__c.month() == 7) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Cash_Carry_Sales_July__c += thdsales.Amount__c;      
  }
  //T CC August
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Cash & Carry') && 
  thdsales.Month_Year__c.month() == 8) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Cash_Carry_Sales_August__c += thdsales.Amount__c;      
  }
  //T CC September
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Cash & Carry') && 
  thdsales.Month_Year__c.month() == 9) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Cash_Carry_Sales_September__c += thdsales.Amount__c;      
  }
  //T CC October
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Cash & Carry') && 
  thdsales.Month_Year__c.month() == 10) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Cash_Carry_Sales_October__c += thdsales.Amount__c;      
  }
  //T CC November
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Cash & Carry') && 
  thdsales.Month_Year__c.month() == 11) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Cash_Carry_Sales_November__c += thdsales.Amount__c;      
  }
  //T CC December
  if((thdsales.SFDC_Account_ID__c == accountId) && (thdsales.POS_Order_Type__c == 'Cash & Carry') && 
  thdsales.Month_Year__c.month() == 12) {
      updateAccounts.get(thdsales.SFDC_Account_ID__c).Total_Cash_Carry_Sales_December__c += thdsales.Amount__c;      
  }
    
    }
    }
  // Update all the accounts with new values.
  Database.update(updateAccounts.values());
}

 

trigger Behr_AmountOfMonths_US on THD_Sales__c (after insert,after update) {
    
    Set<Id> accset = new Set<Id>();
    Set<Id> thdset = new Set<Id>();
    List<THD_Sales__c> thdlst = new List<THD_Sales__c>();
    Map<Id,THD_Sales__c> thdmap = new Map<Id,THD_Sales__c>();
   map<Id,Double> AccountMap = new map <Id,Double>();

    for(THD_Sales__c thd : Trigger.new) {
        if(thd.POS_Order_Type__c == 'Special Order') {
            accset.add(thd.SFDC_Account_ID__c);            
        }
    }
    for(AggregateResult q:[SELECT SFDC_Account_ID__c,sum(Amount__c)tot FROM THD_Sales__c where SFDC_Account_ID__c in :accset group by SFDC_Account_ID__c]) {
        AccountMap.put((Id)q.get('SFDC_Account_ID__c'),(Double)q.get('expr0'));
    }    
}

  Error: Compile Error: line 15:69 no viable alternative at character '' at line 15 column 69

 

AccountMap.put((Id)q.get('SFDC_Account_ID__c'),(Double)q.get('expr0'));