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
Test User 15Test User 15 

Task should be closed whenever Account close task updated

I created one Close Task checkbox in Account. Whenever I checked on Close Task in Account, all task should be closed.
I'm learning how to write triggers. I wrote a trigger but it's not working. Please look at it and help.
 
trigger CloseTaskInAccount on Account (before update) {
    List<Account> acc = [Select Id From Account];
    for(Account acc : Trigger.new) {
        Task t = new Task();
        if(acc.Close_Task__c == True) {
            t.Status = 'Completed';
            t.WhatId = acc.Id;
            update t;
        }
    }
}
Best Answer chosen by Test User 15
Ahmad J. KoubeissyAhmad J. Koubeissy
trigger CloseTaskInAccount on Account (after update) {
    //when dealing with triggers, you hsould consider the bulk operations, thats mean updating many accounts in the same transaction. the updated accounts exists in Trigger.new list, so you dont need to select all the accounts in the database
    set<ID> AccountsID = new set<ID>();
    for(Account acc : Trigger.new) {
       //i did this check to take only the ids of the accounts that have  Close_Task__c=true
       //you can optimize this check by only taking the accounts where the old value of Close_Task__c was false and the new value is true
       if(acc.Close_Task__c) {
            AccountsID.add(acc.id);
        }
    }   
    // here i select all the taks related to the filtered accounts
     for(task t : [select id,status from task where whatid in : AccountsID])
    {
        t.Status = 'Completed';//set the status as completed
        update t;// this DML should not be in a for loop to avoid reaching the limits, so please ill post another version of the code, use it
    }
}
!! USE THIS VERSION !!!:
 
trigger CloseTaskInAccount on Account (after update) {
    
    set<ID> AccountsID= new set<ID>();
    for(Account acc : Trigger.new) {
        if(acc.Close_Task__c) {
            AccountsID.add(acc.id);
        }
    }   
   list<task> tasks = new list<task>();
    for(task t: [select id,status from task where whatid in : AccountsID])
    {
        t.Status = 'Completed';
        tasks.add(t);
    }
    update Tasks;
}

 

All Answers

Ahmad J. KoubeissyAhmad J. Koubeissy
trigger CloseTaskInAccount on Account (after update) {
    
    set<ID> AccountsID= new set<ID>();
    for(Account acc : Trigger.new) {
        if(acc.Close_Task__c) {
            AccountsID.add(acc.id);
        }
    }   
    for(task t: [select id,status from task where whatid in : AccountsID])
    {
        t.Status = 'Completed';
    }
    update Tasks;
}
If this solves your issue, kindly mark it as Best Answer, Thank you.
Raj VakatiRaj Vakati
Hi , 
Please use this code 
 
trigger acct on Contact (after insert,after update) {
    Map<Id,Account> acc = new Map<Id,Account>([Select Id , Name from Account where id in: trigger.newMap.keyset() AND Close_Task__c =TRUE]) ;
    List<Task> allTasks = [select id, Status  from task where whatid in :acc.keySet()];
    for(Task t:allTasks){
        t.status='Completed' ; 
    }
    
    if(allTasks.size()>0){
        update allTasks ;
    }
}

 
Test User 15Test User 15
@Ahmad J. Koubeissy
It's working fine. Can you please explain it to me?
 
trigger CloseTaskInAccount on Account (after update) {
    
    set<ID> AccountsID = new set<ID>();
    for(Account acc : Trigger.new) {
        if(acc.Close_Task__c) {
            AccountsID.add(acc.id);
        }
    }   
     for(task t : [select id,status from task where whatid in : AccountsID])
    {
        t.Status = 'Completed';
        update t;
    }
}



Thanks
Ahmad J. KoubeissyAhmad J. Koubeissy
trigger CloseTaskInAccount on Account (after update) {
    //when dealing with triggers, you hsould consider the bulk operations, thats mean updating many accounts in the same transaction. the updated accounts exists in Trigger.new list, so you dont need to select all the accounts in the database
    set<ID> AccountsID = new set<ID>();
    for(Account acc : Trigger.new) {
       //i did this check to take only the ids of the accounts that have  Close_Task__c=true
       //you can optimize this check by only taking the accounts where the old value of Close_Task__c was false and the new value is true
       if(acc.Close_Task__c) {
            AccountsID.add(acc.id);
        }
    }   
    // here i select all the taks related to the filtered accounts
     for(task t : [select id,status from task where whatid in : AccountsID])
    {
        t.Status = 'Completed';//set the status as completed
        update t;// this DML should not be in a for loop to avoid reaching the limits, so please ill post another version of the code, use it
    }
}
!! USE THIS VERSION !!!:
 
trigger CloseTaskInAccount on Account (after update) {
    
    set<ID> AccountsID= new set<ID>();
    for(Account acc : Trigger.new) {
        if(acc.Close_Task__c) {
            AccountsID.add(acc.id);
        }
    }   
   list<task> tasks = new list<task>();
    for(task t: [select id,status from task where whatid in : AccountsID])
    {
        t.Status = 'Completed';
        tasks.add(t);
    }
    update Tasks;
}

 
This was selected as the best answer
Surya Prakash TomarSurya Prakash Tomar

Hello there,

 

I believe DML operation did not work with the before trigger event.

Could you please try this at a different trigger event. 

Test User 15Test User 15
@Ahmad J. Koubeissy

Thanks sir, nicely explained and updated version is working fine.