+ Start a Discussion
nmnbawanmnbawa 

Task mark auto complete

Hi Everyone,

 

Can someone please help.

 

I have a custom object called Student and has associated tasks with it. 

 

when a student is not active, I want all the tasks to be automatically marked as completed. can someone please help with the trigger and test class.

 

Many many thanks in advance.

Best Answer chosen by Admin (Salesforce Developers) 
SFAdmin5SFAdmin5

here you go.  note this is an updated trigger from my original one.  

 

note also  that i am assuming your Student record's "Active" custom field is a checkbox.  If it is some other field type then change the map value from boolean to String or whatever the type is

 

trigger updateTaskStatus on Student__c (after update) 
{   
        List<Student__c> studentList = new List<Student__c>([SELECT Id,Active__c FROM Student__c WHERE Active__c = false]);                 
        Map<Id,Boolean> taskMap = new Map<Id,Boolean>();
            For(Student__c s: studentList)
            {
                taskMap.put(s.ID,s.Active__c);
            } 
            
        List<Task> taskList = new List<Task>();
            For(Task t :  [select Id,WhatId,Status from Task where WhatId IN: taskMap.Keyset()])
            {
                t.Status='Completed';
                taskList.add(t);
            } 
                Update taskList;                             
}

 

All Answers

SFAdmin5SFAdmin5

here you go.  note this is an updated trigger from my original one.  

 

note also  that i am assuming your Student record's "Active" custom field is a checkbox.  If it is some other field type then change the map value from boolean to String or whatever the type is

 

trigger updateTaskStatus on Student__c (after update) 
{   
        List<Student__c> studentList = new List<Student__c>([SELECT Id,Active__c FROM Student__c WHERE Active__c = false]);                 
        Map<Id,Boolean> taskMap = new Map<Id,Boolean>();
            For(Student__c s: studentList)
            {
                taskMap.put(s.ID,s.Active__c);
            } 
            
        List<Task> taskList = new List<Task>();
            For(Task t :  [select Id,WhatId,Status from Task where WhatId IN: taskMap.Keyset()])
            {
                t.Status='Completed';
                taskList.add(t);
            } 
                Update taskList;                             
}

 

This was selected as the best answer
SFAdmin5SFAdmin5

and the test class...

 

@isTest
    private class testUpdateTaskStatus 
    {
        static testMethod void verifyUpdateTaskStatus ()
        {
        
        Student__c s = new Student__c ();
        s.Name = 'test';
        s.Active__c =true;
        insert s;
        
        Task t = new Task();
        t.Subject = 'test';
        t.Status = 'In Progress';
        t.Priority = 'Low';
        t.WhatId = s.Id;
        insert t;
        
        s.Active__c = false;
        update s;

        List<Task> retrieveTask = [SELECT id,WhatId,Status FROM Task WHERE Whatid =: s.Id ];
        
        Integer size = retrieveTask.size();
        system.assertEquals(1,size);            

               
        }
    }

 

nmnbawanmnbawa

Thanks a lot for your reply. 

 

The field is not called Active. Its called status.

 

The values I am considering is Status  = "Expelled,Dropped"

 

Please help 

 

Many thanks again