I have a trigger that is counting the number of 'Open' tasks on an Account object.

The trigger is working correctly when I add a new Task to an account, however when I 'close' the task the update is not working correctly.

E.g. When I add a new task to my account it increments Number_of_Open_Tasks__c by 1 which is what should happen, and when I close a task Number_of_Open_Tasks__c stays at 1 for the account when it really should be 0.

The odd thing is in my logs I can see that the value has change and is being set in the update method, but the value is not actually updating.

I'm at a loss here.

Any suggestions are appreciated

trigger UpdateAccountOpenTasksTrigger on Task (after delete, after insert, after undelete, after update) {

    public List<Task> taskList = new List<Task>();
    public id accountId;
    public integer openNumber = 0;

    if (Trigger.isInsert || Trigger.isUnDelete || Trigger.isUpdate) {

        for (Task t : Trigger.New) {
            accountId = t.WhatId;
            System.debug(LoggingLevel.INFO, 'Account Id: ' + accountId);


        taskList = [SELECT id, Status FROM Task WHERE WhatId = :accountId];
        System.debug(LoggingLevel.INFO, 'TaskList size: ' + taskList.size());

        for (task t : taskList) {
            System.debug(LoggingLevel.INFO, 'Current Task: ' + t);
            System.debug(LoggingLevel.INFO, 'Current Task Status: ' + t.Status);
            if (!t.Status.equals('Closed')) {
                System.debug(LoggingLevel.INFO, 'Updating Open Task Number');
                openNumber = openNumber + 1;

        List<Account> accountListToUpdate = new List<Account>();

        List<Account> accountListToCheck = [SELECT id, Number_of_Open_Tasks__c FROM Account WHERE id = :accountId];

        System.debug(LoggingLevel.INFO, 'Number of accounts to check: ' + accountListToCheck.size());

        for (Account a : accountListToCheck) {
            System.debug(LoggingLevel.INFO, 'Checking Account: ' + a);
            System.debug(LoggingLevel.INFO, 'Current Open Task Number: ' + openNumber);
            System.debug(LoggingLevel.INFO, 'Current Number of Open Tasks: ' + a.Number_of_Open_Tasks__c);
            a.Number_of_Open_Tasks__c = openNumber;
            System.debug(LoggingLevel.INFO, 'Changed Number of Open Tasks: ' + a.Number_of_Open_Tasks__c);

        if (accountListToUpdate.size() > 0) {
            System.debug(LoggingLevel.INFO, 'Updating Accounts ... ');
            update accountListToUpdate;

Here are the logs showing the value is being (should be?) updated:

15:57:29.1 (794198586)|USER_DEBUG|[48]|INFO|Current Number of Open Tasks: 1
15:57:29.1 (794284374)|USER_DEBUG|[50]|INFO|Changed Number of Open Tasks: 0
15:57:29.1 (794310584)|USER_DEBUG|[55]|INFO|Updating Accounts ...
15:57:29.1 (794353764)|DML_BEGIN|[56]|Op:Update|Type:Account|Rows:1

The root cause was in this line of your code:

if (!t.Status.equals('Closed')) {...}

Upon going through Task's Status field, could see that the actual value is Completed and that this value determines a Closed Task. So if you change the value from Closed to Completed, you will get the results as desired.

Got this working by copying your code and changing the value to Completed and it worked fine.

if (!t.Status.equals('Completed')) {...}

Also below is a screenshot of how the Status fields belong to a particular Category for Task.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.