function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Vasu@blrVasu@blr 

My trigger is not working for bulk regards

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

 

Best Answer chosen by Admin (Salesforce Developers) 
Vasu@blrVasu@blr

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

 

Dataloader time settings should be equal to sandbox or production time zone setting then only it will mark properly.