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
Andrew marshal 3Andrew marshal 3 

Trigger on update , insert and delete

Any one please advise me how to write a trigger that will send an email to alert when that record is created or updated or deleted.
Eswar Venkat 2Eswar Venkat 2
trigger SendEmailOnRecordChange on Custom_Object__c (after insert, after update, after delete) {

    String subject = 'Record Change Alert';
    String body = 'A record has been created, updated, or deleted.';
    List<String> toAddresses = new List<String>{''};
    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
    Messaging.SendEmailResult[] results = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
SubratSubrat (Salesforce Developers) 
Hello Andrew ,

Can you specify the parent object on which you want your trigger on ?

Thank you.
Prateek Prasoon 25Prateek Prasoon 25
trigger SendEmailOnRecordChange on Object__c (after insert, after update, after delete) {
  List<Object__c> changedRecords = new List<Object__c>();
  for(Object__c record : {
  for(Object__c record : Trigger.old) {
  if(changedRecords.isEmpty()) {
  Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
  email.setToAddresses(new List<String>{''});
  email.setSubject('Record Change Alert');
  email.setPlainTextBody('The following records have been created, updated or deleted:\n\n');
  for(Object__c record : changedRecords) {
    email.setPlainTextBody(email.getPlainTextBody() + 'Record ID: ' + record.Id + '\n');
  Messaging.sendEmail(new List<Messaging.SingleEmailMessage>{email});

If you find this answer helpful, Please mark it as the best answer.
Arun Kumar 1141Arun Kumar 1141
Hello Andrew,
This trigger will help you to send email to all the contacts in the record i.e the emails mentioned in the record of that object. 

trigger EmailObjectTrigger on Object__c (After insert,After update,After delete) {
list<String> emailAddress=new list<String>();
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setSubject('Record Change Alert');
        mail.setPlainTextBody('The record has been created,deleted or updated.');
        Messaging.sendEmail(new List<Messaging.SingleEmailMessage>{mail});

I hope this helps you!