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
Ashu sharma 38Ashu sharma 38 

Trigger is not working when dataloader is updating

Hi,

I am trying to update records using data loader but not update,but when i do on single record its been updating.

I am written an trigger on task.
public class UpdateActivityHistoryCallCount {
    
    
    //To update the Count of Activity History in Lead object
    
    public static void countActivityHistoryOnLead(list<Task> newTaskList,list<Task> oldTaskList){
        system.debug('<<<<<test>>>>>');
        set<Id> LeadIds=new set<Id>();
        list<Lead> leadList=new list<Lead>();
        // Map<Id, String> error = new Map<Id, String>();
        
        if(trigger.isInsert || trigger.isUnDelete ){
            for(Task tsk:newTaskList){
                if(tsk.WhoId!=null){
                    if(string.valueOf(tsk.WhoId).startsWith('00Q'))
                        LeadIds.add(tsk.WhoId);
                    
                    
                }
                system.debug('Lead Ids' +LeadIds);
            }
        }
        if(trigger.isDelete || trigger.isUpdate){
            for(Task tk:oldTaskList){
                if(tk.WhoId!=null){
                    if(string.valueOf(tk.WhoId).startsWith('00Q'))
                        LeadIds.add(tk.whoId);
                    
                }
            }
            system.Debug('Test LeadIds' +LeadIds);
        }
        if(LeadIds.size()>0){
            for(Lead l:[select id,(select id,subject,Due_Date__c,CreatedDate
                                   from tasks where (subject='Call' and status='Completed'
                                                     and  Due_Date__c < Today) or (subject='Call' and status='Completed' and Due_Date__c =  null)  ) 
                        from lead where id in :LeadIds ])
                leadList.add(new lead(id=l.Id,Call_count__c=l.tasks.size()));
             update leadList;
            
            system.debug('Test');
        }
       
        
    }
    
    
@ M  Coder@ M Coder
Hello Ashu sharma, 

You need to bulkify the scenario using Maps . 

please tell the scenario of the trigger and whats your desired output to bulkify such that any one could help you


 
Ashu sharma 38Ashu sharma 38
Hello, 

I  am trying to update the count the number of activity attached (task object) to a particular lead. 

So how can we do using map. 

Thanks.. 
@ M  Coder@ M Coder
Hello Ashu , 

i used aggregate result and tested it is working perfectly fine , used a new field on lead.No_of_Tasks__c


trigger ActivityCountToLead on Task (before insert,before update,after insert,after update,after delete, after Undelete) {
set<id> taskIdset = new set<id>();
if(trigger.isInsert || trigger.isUpdate || trigger.isUnDelete ){
        for(Task tsk: trigger.New)
        {
            system.debug('New called===');
            if(tsk.WhoId!=null)
            {
             if(string.valueOf(tsk.WhoId).startsWith('00Q'))
             taskIdset.add(tsk.WhoId);
             system.debug('taskIdset==='+taskIdset);
            }
        }    
    }
    
    if(trigger.isDelete){
        for(Task tsk: trigger.old)
        {
           system.debug('Old called===');
            if(tsk.WhoId!=null)
            {
             if(string.valueOf(tsk.WhoId).startsWith('00Q'))
             taskIdset.add(tsk.WhoId);
             system.debug('taskIdset==='+taskIdset);
            }
        }    
    }  


 if(taskIdset.size() >0)
  {
    List<AggregateResult> lstResult = [SELECT whoId, COUNT(Id) countId FROM Task WHERE whoId IN:taskIdset AND ((subject='Call' and status='Completed' and  Due_Date__c < Today) OR (subject='Call' and status='Completed' and Due_Date__c=null)) GROUP BY whoId ];
    List<Lead> lstlead = new List<Lead>();
        for(AggregateResult result:lstResult)
         {
            Lead l= new lead(Id=(Id)result.get('whoId'), No_of_Tasks__c= (Integer)result.get('countId'));
            system.debug('l====='+l);
            lstlead.add(l);
         }
      update lstlead ;  
 }

}