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

Trigger unable to update old records into custom field



I have a trigger OpenActivityCount to get the count of the open tasks created on the open activity related list. The count of the tasks appear on the No. of Open Activities field.


I am getting a problem here.


The issue is all the new tasks created after I have deployed the tigger are getting populated in the field whereas the older tasks are not being populated. I want the total count of the open tasks in the field before and after I have deployed the trigger.


Can anyone please tell me how to overcome this issue.


This is the trigger


trigger OpenActivityCount on Task(after insert,after update)
Account ac=[select id, No_of_Open_Activities__c from Account where[0].AccountId];
if(ac.No_of_Open_Activities__c == null)
ac.No_of_Open_Activities__c =0;
if(trigger.isinsert) {
if([0].status!='Completed') {
ac.No_of_Open_Activities__c +=1;

if(trigger.isupdate )
if([0].status == 'Completed')
ac.No_of_Open_Activities__c -= 0.5;
if([0].status != 'Completed')
ac.No_of_Open_Activities__c += 0.5;

update ac;




Hi ,

Try below given code and see if it helps youi.It is on the lead object.


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:{
            System.debug('WhoId = ' + t.WhoId);
    // Update the Leads
        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_Tasks__c = l.Tasks.size()));
        update LeadsToUpdate;   



I am not sure if this one works as I need to do an update only once.


Please correct me if wrong.