Friday, 13 November 2015

Task Due Date populating

Pre-Populate the Due Date field in Salefsorce Task Object

I was recently working with a customer who wanted to automatically the ActivityDate or Due Date field on the Task Object automatically.

Unfortunately, at this moment in time (Winter '16) you cannot access the ActivityDate or "Due Date" via Formula or Workflow so this solution required an Apex Trigger.

The Trigger was set up to only apply the Due Date fix to Task records that were being created by the customer's CTI solution.

The Trigger

Here is the full Trigger that I used.

trigger TaskDueDate on Task (before insert) {
    for(Task myTask : trigger.new)  
    {
        if (myTask.ActivityDate == null)
        {
            if (myTask.CallType == 'Inbound' || myTask.CallType == 'Inbound')
            {
                myTask.ActivityDate = System.today();
            }
        }
    }   
}

The Test Class

Here is the Test Class.

@isTest
public class TestTaskDueDate {
    
    @isTest public static void testDueDate() {      
        
        // Create dummy task record          
        Task a = new Task(
            CallType = 'Inbound',
            Subject = 'Test',
            Status = 'Not Started',
            Priority = 'Normal',
            CallDurationInSeconds = 33 // ,
        );
        insert a;
        
        Task b = [SELECT Id, ActivityDate from Task WHERE id =: a.Id];
        
        System.assertEquals(System.today(), b.ActivityDate);
        System.debug('Date Date is ' + b.ActivityDate);
        
    } //end of test method
    
} //end class

Icon by Everaldo Coelho from Iconfinder