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
Antonio_hotelbedsAntonio_hotelbeds 

How to retrieve the in progress and completed activities of an account?

Hi there,

I need to retrieve the tasks related to an account having the account id.

I am developing a trigger that, when an activity is deleted has to check if there is any other activity for that account in order to update the field "Evolution stage". If the last activity is deleted the evolution stage must go back to "Agency to be contacted".Here is my code:

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

    if (Trigger.isDelete){
           List<Task> tasks = new list<task>();
            tasks= Trigger.old;   
            if (tasks.size()== 1){
               List<ID> acctIds = new List<ID>();
                for (Task tk : tasks) {
                    acctIds.add(tk.AccountId);
                }  
               Account account =new Account(Id=acctIds.get(0));

             //HERE I NEED TO CHECK IF IT HAS MORE ACTIVITIES TO DO THE FOLLOWING OR NOT
            task[] rest=[select id from task where AccountId ????????]
            if (rest.size()==0){//IF THERE ARE NO MORE ACTIVITIES DO THE FOLLOWING

                if (account.Evolution_Stage__c=='4 - Bookings done'){
                   No hacer nada    
                }else if (account.Access_codes_sent__c==NULL){
                    account.Evolution_Stage__c='1 - Agency to be contacted';
                    update account;
                }else{
                 /  account.Evolution_Stage__c='2 - Access codes created';
                   update account;
               }
             }           
             }
    }
    else{
    Task[] tasks;

        tasks= Trigger.new;
       
    // creo un conjunto set de ID   
    List<ID> acctIds = new List<ID>();  
    //cargo este set con los id de las cuentas de los eventos  
    for (Task tk : tasks) {
        if ((tk.Status=='Completed') & (tk.BOL_Level_III_Result__c!='')){
            acctIds.add(tk.AccountId);
        }
    }   //ahora acctIds es una lista sin repeticiones de identificadores de cuentas
   
    //Actualizar los evolution stage de las cuentas si cumplen las condiciones
    for(Integer i=0; i< acctIds.size();i++){
        Account account =new Account(Id=acctIds.get(i));
        if (account.Evolution_Stage__c!='4 - Bookings done'){
            account.Evolution_Stage__c='3 - Agency Contacted (Visit / Call)';
            update account;
        }
    }
    }

}

I got completely stucked in the part where I want to check if there is any other activity related to the account. could you please help me?Any ideas??

Thanks a lot for your help!!

Best Answer chosen by Admin (Salesforce Developers) 
hisrinuhisrinu

Here is the query

 

select id from Task where whatId in :accountIds and Status not in ('Completed','In Progress')

 

This will retrieve all the tasks which are related to this account where the status not in Completed and In Progress

All Answers

hisrinuhisrinu

Here is the query

 

select id from Task where whatId in :accountIds and Status not in ('Completed','In Progress')

 

This will retrieve all the tasks which are related to this account where the status not in Completed and In Progress

This was selected as the best answer
Antonio_hotelbedsAntonio_hotelbeds

Hi Srini,

 

First of all thanks very much for your help, it was really useful.

 

I have another new problem that I will open in a new discussion.