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
wedaftwedaft 

Sent email triggers a field update in the record that the email is related to.

I have a custom object called Observation Summary (Observation_Summary__c), and within that object I have a field called Date of Assessment (Date_of_Assessment__c). I would like to create a trigger that does the following:

 

Whenever an email is sent from the Observation Summary record, and that email's subject begins with "Observation feedback", the Date of Assessment field on the Observation Summary should update to become the date that the email was sent.

 

Is this possible? Thanks for your help!

Saikishore Reddy AengareddySaikishore Reddy Aengareddy

Assuming you are using Standard "Send Email" button to send email from Activity History related list. Once you send an email a task will be created. So create a trigger on task object and check in trigger if "WhatId" is related to your desired object and then update the  date of assesment appropritely to current date.

 

Hope this helps.

wedaftwedaft

Would the trigger go on the task object or the observation summary object? I thought it would go on the obeservation summary object.

Saikishore Reddy AengareddySaikishore Reddy Aengareddy

It would go on task object. Once you send an email on a observation record a task willl be created with subject as Email:subject...

 

Look at this sample code... this might help you

/*Trigger*/
trigger ICG_TaskTrigger on Task (after insert) {
    
  ICG_TaskTriggerHandler handler = new ICG_TaskTriggerHandler();
        
    if(Trigger.isInsert && Trigger.isAfter){
        handler.OnAfterInsert(Trigger.new);
    }
}
/*Class*/
public without sharing class ICG_TaskTriggerHandler {
  
  public void OnAfterInsert(Task[] newTasks){
    processLastEmailDate(newTasks);
  }
  private void processLastEmailDate(Task[] newTasks){
    Set<ID> ObservationIds = new Set<ID>();
List<observation_Summary__c> obsList = new List<observation_Summary__c>();
    
    for(Task task:newTasks){
      String subject = task.Subject;   
      String description = task.Description;
      if ( subject != null && subject.startsWith('Email: Observation feedback')) {
        if(task.WhatId!=null && isObservation(task.WhatId)){
          ObservationIds.add(task.WhatId);
        }
      }
    }
    if(observationIds.size()>0){
for(Observation_Summary__c ob: [select Id,Date_of_Assessment__c from Observation_Summary__c where Id IN:ObservationIds]){
ob.Date_of_Assessment__c = system.Today();
obsList.add(ob);
}
 
update obList;
}
  }
  private boolean isObservation(String id){
        // first 3 digits of the recordId of any observation summary record... replace these 3 digits properly
if(id.startsWith('a06'))   
                return true;
        return false;
    }
   
}

 

Noah DiPasqualeNoah DiPasquale
Hello I've a custom object where Emdate__C, Mailto__c, Subject__C, Body__c are supposed to be updated when ever a new email is sent using APEX code and by not using the standard "Send Email" button help me understand how to it as the above provided code is not working when used.