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
Tobias HaggeTobias Hagge 

Debug simple apex trigger task to account

Hey, hope someone could help debugging this code. Every email we sent to customers is bcc'd to Salesforce, so they are being attached to the relevant account/contact. For one specific email, we would like to display the time the email has been sent to the customer. This works fine in any simple/manual test, but fails in production on a significant big amount of customer who receive the email (30%+), in which case the datestamp is just missing.
I'm not sure if we exceed any limits (no error message) and don't know how to debug, as the trigger works in theory. Thanks for the help, all if, else if conditions are mutual exclusive.
 
trigger Trigger_Task2Account on Task (after insert, after update) { //trigger name, on which object, when does it fire 
  Task[] tasks = Trigger.New;
  
  Map<String, Task> accountMap = new Map<String, Task>();
  
  for(Task task: tasks)
  {
    String accountId = task.AccountId; // maps object where triggers fire to object where result resides
    if (accountId != null)
      accountMap.put(task.WhatId, task);
  }
  List<Account> accounts = new List<Account>([Select Id, Doc_Chaser__c, Last_Call_Purpose__c, Last_Call_Outcome__c , Last_Call_Date__c, Latest_Opportunity__c, Required_Documents_Email__c, Last_Call_Result__c, Customer_Process__c FROM Account WHERE Id in :accountMap.keySet()]);
  
  for(Account account: accounts)
  {   
        Task task = accountMap.get(account.Id);
        if(task.Purpose__c != null || task.Call_Outcome__c != null) //conditions 
        {
            account.Last_Call_Purpose__c = task.Purpose__c;  //what: object.field__c, true, false, 'string', datetime.function
            account.Last_Call_Outcome__c = task.Call_Outcome__c;
            account.Last_Call_Date__c = task.CreatedDate;
        }
        
        else if(task.Subject == 'Email: Completing your iwoca application...')
        {
          account.Required_Documents_Email__c = datetime.NOW();
        }
        
        else if(task.Subject == 'Telesales Call')
        {
          if(task.CallDisposition == 'Sale - Application Review' && account.Customer_Process__c == '5 - Live in Dialler')
          {
            account.Last_Call_Result__c = task.CallDisposition; //CallDisposistion is API name for Call Result
            account.Last_Call_Date__c = task.CreatedDate;
            account.Customer_Process__c = '1 - Application Review';
            update account;
          }
          else
          {
          account.Last_Call_Result__c = task.CallDisposition; //CallDisposistion is API name for Call Result
          account.Last_Call_Date__c = task.CreatedDate;
          }
        }
    }
    update accounts;
}

 
Tobias HaggeTobias Hagge
Oh the relevant statement is row 24 - 27.
Thomas Graves 1Thomas Graves 1
did you ever figure this out?

I'm running into a similar problem where tasks created from bcc do not get processed by my trigger but manually created tasks do. This is because many fields show up as null on the task that SF creates.