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
David89David89 

how to send an email with apex trigger (before delete)

If anybody delete my record from "Dipendenti__c" send notification to that mail.
In this code mail is not sending.
Please verify the code

trigger mailDelDip on Dipendente__c (before delete) {
    Messaging.reserveSingleEmailCapacity(trigger.size);
    List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
    for (Dipendente__c dip : Trigger.old){
    
    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 
    email.setToAddresses(new String[] {'montorselli.daniele@gmail.com'}); 
    email.setSubject('Automated email: dipendente eliminato');    
    email.setPlainTextBody('Attenzione è stato cancellato un dipendente');
    emails.add(email);         
        }
    Messaging.sendEmail(emails);
    for (Dipendente__c dip : Trigger.old){
        dip.addError('Unable to delete record!');
    }
    }
Srinivas SSrinivas S
Hi David,

1. Please track why it is failing, I have added an exception block -
trigger mailDelDip on Dipendente__c (before delete) {
	Messaging.reserveSingleEmailCapacity(trigger.size);
	List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
	for (Dipendente__c dip : Trigger.old){
		Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 
		email.setToAddresses(new String[] {'montorselli.daniele@gmail.com'}); 
		email.setSubject('Automated email: dipendente eliminato');    
		email.setPlainTextBody('Attenzione è stato cancellato un dipendente');
		emails.add(email);         
	}
	try {
		Messaging.sendEmail(emails);
	}
	Catch(EmailException emailExp) {
		system.debug('Exception: '+emailExp);
	}
	for (Dipendente__c dip : Trigger.old){
		dip.addError('Unable to delete record!');
	}
}

2. If you are workiing in a developer edition, you can send only 15 emails per day to the external users (emails you mention manually).
Note: For users, leads and contacts there will be a different count.

Thanks,
Srinivas
- Please mark as solution if your problem is resolved.
vicky Salesforcevicky Salesforce
Hi David 
Can you check your email setting in the Org ... setup --> email administration --> Deliveribilty: Access level :ALL Email. 
 code should be good to send out an email. 

thank you
 
David89David89
Maybe I was wrong to explain my issue. 
I need a trigger to avoid to delete a record from a custom object 
(Dipendente__c) and send me an email.

This trigger works (avoid delete), but I don't receive any email!

I wrote this!

trigger AvoidDelete on Dipendente__c (before delete) {

    for (Dipendente__c dip : Trigger.old){
    if (dip.Contratto_attivo__c==true){
        
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
    String[] toAddresses = new String[] {'xxxx.xxxx@gmail.com'}; 
    mail.setToAddresses(toAddresses); 
    mail.setSubject('Automated email: delete a record'); 
    String body = 'Somebody try to delete a record';    
    mail.setPlainTextBody(body);       
    Messaging.sendEmail(new Messaging.SingleEMailMessage[]{mail});    
        
    dip.addError('You can't delete this record!');    
    }
    }
}