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
Aishwarya P 4Aishwarya P 4 

Please help me write the class.

When an Opportunity is deleted, a mail should be generated to inform the Owner of the Account informing him about this.
//Trigger:
trigger EmailOnDelete on Opportunity (after delete) {
    Messaging.reserveSingleEmailCapacity(trigger.size);
    List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
    for (Opportunity opp : Trigger.old) {
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        email.setToAddresses(new String[] {'ash@gmail.com'});
        email.setSubject('Trigger on Oppurtunity Activated : Deleted Account Alert');
        email.setPlainTextBody('This message is to alert you that the account named ' + opp.Name + ' has been deleted.');
        emails.add(email);
    }
    Messaging.sendEmail(emails);
}


I'm able to write the trigger, But i want a class method to be called in trigger and all the actions to happen in the class method. 
GauravGargGauravGarg
Hi Aishwarya,

Please create another Class and call this class from trigger by passing (trigger.new, trigger.oldMap). 
Public Class opportunityEmailToOwner {
    public void sendEmailToOpptyOwner(List < Opportunity > NewOpptyList, Map < Id, Opportunity > OldOpptyMap) {
        Messaging.reserveSingleEmailCapacity(trigger.size);
        List < Messaging.SingleEmailMessage > emails = new List < Messaging.SingleEmailMessage > ();
        for (Opportunity opp: OldOpptyMap) {
            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
            email.setToAddresses(new String[] {
                'ash@gmail.com'
            });
            email.setSubject('Trigger on Oppurtunity Activated : Deleted Account Alert');
            email.setPlainTextBody('This message is to alert you that the account named ' + opp.Name + ' has been deleted.');
            emails.add(email);
        }
        Messaging.sendEmail(emails);
    }
}


Let me know if you still face any issue, or you can directly contact me on below details.

Thanks,

Gaurav
Skype: gaurav62990