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
Abilash.SAbilash.S 

Trigger to log a call when date changed on Account obj. When date is changed a task(log a call) should create.

Trigger on whenever account is updated the date field is changed then log a call with that date.

I am strucked on 
1. How to check date field is changed or  not. I need to trigger when existing date changed.
2. Unable to create activity (log a call) on record page. In debug logs getting data to insert. But on record page Call is not logging from apex.
Please help me in code.

// log a call when date field changed in account
    public static void logCall(List<Account> newMapps) {
        List<Task> tkList = new List<Task>();
            Set<Id> regIds = new Set<Id>();
        for(Account ac: newMapps){
            regIds.add(ac.Id);
        }
        
     Map<Id, Account> mapAcc = new Map<Id, Account>([select id, name, support_plan_start_date__c FROM Account WHERE ID IN :regIds]);
            system.debug('mapAcc'+mapAcc);
        for(Account a : newMapps){
            system.debug('77'+a.support_plan_start_date__c);
            system.debug('78'+mapAcc.get(a.Id).support_plan_start_date__c);
            if(a.support_plan_start_date__c == mapAcc.get(a.Id).support_plan_start_date__c || a.support_plan_start_date__c >mapAcc.get(a.Id).support_plan_start_date__c || a.support_plan_start_date__c <mapAcc.get(a.Id).support_plan_start_date__c){
                  Task newtask = new Task();
            newtask.Description = 'Log a call when datefield changes';
            newtask.Priority    = 'Normal';
            newtask.Status      = 'Completed';
            newtask.CallDisposition = 'CallBack';
            newtask.Subject     = 'Call';
            newtask.CallType    = 'OutBound';
            newtask.IsReminderSet = true;
            newtask.ReminderDateTime = System.now()+1;
     //     newtask.WhoId      = Trigger.new[0].Id;
            tkList.add(newtask);  
                }
        }
        system.debug('tkList'+tkList);
        try{
        if(tkList.size() > 0){
            insert tkList;
            }
        } catch (DmlException e) {
            system.debug('Exception caused'+e.getMessage());
        }
    }

Thanks in advance
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

The Handler should be as below.

You have to use oldmap and get oldaccount as higlited and then compare with new value
 
public class LogACall {
 public static void logCall(map<id,Account> oldmap, List<Account> newlist) {
        List<Task> tkList = new List<Task>();
            Set<Id> regIds = new Set<Id>();
        for(Account ac: newlist){
            
            Account oldacc= oldmap.get(ac.id);
            
            if(ac.support_plan_start_date__c!=oldacc.support_plan_start_date__c ){
                Task newtask = new Task();
            newtask.Description = 'Log a call when datefield changes';
            newtask.Priority    = 'Normal';
            newtask.Status      = 'Completed';
            newtask.CallDisposition = 'CallBack';
            newtask.Subject     = 'Call';
            newtask.CallType    = 'OutBound';
            newtask.IsReminderSet = true;
            newtask.ReminderDateTime = System.now()+1;
         newtask.whatid      = ac.Id;
            tkList.add(newtask);  
            }
          
        }
        insert tkList;
     
    }

}

The trigger for the above will be as below.
 
trigger updateDaTE on Account (after update) {

    LogACall.logCall(Trigger.oldmap,Trigger.new);
    
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,