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
anil87anil87 

Trigger to count open activities on opportunity

Hi Everyone,

 

We want to capture the Total Open Activities Events & Task) on the Lead & Opportunity objects, When there are no Open Activities the Total Open Activities field should be 0..How can i achieve this

Vinit_KumarVinit_Kumar

Anil,

 

There are a couple of ways of doing it:-

 

1.)You can create a dummy field(Checkbox) on Lead and Opportunity.Yoou can update it via Data Loader and on the update event you can write Trigger which will count the no. of oppen activities and update it on a custom field.

 

2.) Secondly,you can createa a batch class and schedule it to run at anytime whihc will populate the custom field on Lead and Opportunity.

ForceLoverForceLover

Hi Vineet,

 

I need to get open Activities count(Events+Tasks) my trigger giving only Task count on lead how i include event count  to  my open activities count. How i need to write the trigger on  Event, can you provide me code to include open events to same custom field

 

trigger UpdateLeadOpenTasks on Task (after delete, after insert, after undelete,

after update) {

                

                 // Declare the variables

                 public set<Id> LeadIDs = new Set<Id>();

                 public list<Lead> LeadsToUpdate = new List<Lead>();

                

                 // Build the list of Leads to update

                

                 for(Task t: Trigger.new){

                                     if(string.valueOf(t.WhoId).startsWith('00Q'))

                                                          LeadIDs.add(t.WhoId);

                                                          System.debug('WhoId = ' + t.WhoId);

                 }

                

                 // Update the Leads

                

                 if(LeadIDs.size()>0){

                                     for(Lead l: [Select l.Id, l.Open_Tasks__c,

                                                          (Select Id From Tasks where IsClosed = False)

                                                          From Lead l where Id in :LeadIDs])

                                                          LeadsToUpdate.add(new Lead(Id=l.Id, Open_Activites__c = l.Tasks.size()));

                                                     }

 

                                     update LeadsToUpdate;  

}

Vinit_KumarVinit_Kumar

Hi atelli,

 

Please try below :-

 

trigger updateRelatedActivities on Lead (before update) {

    List<Id> idList =  new List<Id>();
    Integer taskCount = 0 ;
    Integer eventCount = 0 ;
   
    for(lead le : trigger.new){
    idList.add(le.id);    
    }
    
    List<aggregateResult> tskresults = [select count(id) from Task where whoid in:idList and Status!='Completed'];
    
    for (AggregateResult ar : tskresults )  {
   
    taskCount = (Integer)ar.get('expr0');
}
    
    List<aggregateResult> eventresults = [select count(id) from Event where whoid in:idList ];
    
    for (AggregateResult ar : eventresults )  {
   
    eventCount = (Integer)ar.get('expr0');
   
}
     for(lead le : trigger.new){
    le.NumberofLocations__c =  taskCount +  eventCount;  // field on which Task count and event count needs to be updated
    }
    
}