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
Hemalatha  ParuchuriHemalatha Paruchuri 

If any one try to delete my record send email notofication to all profile users

if u want delete my custom object record (estimate) send notofication to all profiles
Best Answer chosen by Hemalatha Paruchuri
Rahul Sangwan7341Rahul Sangwan7341
for (Estimate__c esti : Trigger.old) {
          Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage()
        email.setToAddresses(new String[] {'hemalathaparuchuri8@gmail.com'});
        email.setSubject('They are try to Delete Estimate Alert');
        email.setPlainTextBody('This message is to alert you that the Estimate named ' + esti.Name + ' has been deleted.');
        emails.add(email);
    }
    Messaging.sendEmail(emails);
for (Estimate__c esti : Trigger.old) {
 esti.addError('Unable to delete record!');
    }
}

add error method after email sent otherwise it will break after error and will not send the email.

Please select this as Best Answer if it helps you.

All Answers

Rahul Sangwan7341Rahul Sangwan7341
Hi you can do it through trigger after delete you can send the email through apex code and get all the users to whom you have to send the email.
 
Hemalatha  ParuchuriHemalatha Paruchuri
but i need a dont delete record
NagaNaga (Salesforce Developers) 
Hi Hemalatha,

Can you put in a validation rule  and connect it to a trigger to send an email notification to all the profile users.I am just thinking if is possible.

Best Regards
Naga Kiran
Rahul Sangwan7341Rahul Sangwan7341
yeah you can through an exception also in trigger before delete 1st send a email to all profiles and then through a trigger.error message so that it will not allow user to delete
Hemalatha  ParuchuriHemalatha Paruchuri
trigger Estimateemailalert on Estimate__c (before  delete) {
    Messaging.reserveSingleEmailCapacity(trigger.size);
    List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
    for (Estimate__c esti : Trigger.old) {
          Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        esti.addError('Unable to delete record!');
        email.setToAddresses(new String[] {'hemalathaparuchuri8@gmail.com'});
        email.setSubject('They are try to Delete Estimate Alert');
        email.setPlainTextBody('This message is to alert you that the Estimate named ' + esti.Name + ' has been deleted.');
        emails.add(email);
    }
    Messaging.sendEmail(emails);
}
               


if any body delete my record send notification to that mail.in this code mail is not sending.please verify the code
Rahul Sangwan7341Rahul Sangwan7341
for (Estimate__c esti : Trigger.old) {
          Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage()
        email.setToAddresses(new String[] {'hemalathaparuchuri8@gmail.com'});
        email.setSubject('They are try to Delete Estimate Alert');
        email.setPlainTextBody('This message is to alert you that the Estimate named ' + esti.Name + ' has been deleted.');
        emails.add(email);
    }
    Messaging.sendEmail(emails);
for (Estimate__c esti : Trigger.old) {
 esti.addError('Unable to delete record!');
    }
}

add error method after email sent otherwise it will break after error and will not send the email.

Please select this as Best Answer if it helps you.
This was selected as the best answer
Hemalatha  ParuchuriHemalatha Paruchuri
using this code also i didn't get any email
Rahul Sangwan7341Rahul Sangwan7341
it is working fine?
 
Hemalatha  ParuchuriHemalatha Paruchuri
Now also i didn't get any email
Hemalatha  ParuchuriHemalatha Paruchuri
record is not delete but i didn't get any mail
Rahul Sangwan7341Rahul Sangwan7341
yeah..............you can do 1 thing create a custom delete button and replace it with standard delete button.
Then on custom button show the alert message and send the email through workflow.
Hemalatha  ParuchuriHemalatha Paruchuri
delete is already custom button
Rahul Sangwan7341Rahul Sangwan7341
Then you can just update 1checkbox on record and from workflow send the email .
shaik yusufshaik yusuf
trigger EmailConfirmation on Account (after delete) {
  List<id> myids = new List<id>();
    List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
    for(schema.Account acc: trigger.old){
        
        Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage(); 
        msg.setToAddresses(new String[]{acc.Email__c});
        msg.setSubject('Confirmation from Salesforce');
        msg.setPlainTextBody('Hi'+acc.Name+'The record is Successfully Deleted');
        //Messaging.sendEmail(Messaging.SingleEmailMessage{msg});
        emails.add(msg);
    }
    Messaging.sendEmail(emails);
}

Try above code, it definetly Works