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
Lakshmi SLakshmi S 

Sending email using before delete Trigger.

Hi Team,

I want to send an email using before delete trigger (Record details).
Trigger :
trigger DeleteNotify on Custom__c (Before Delete) {
    
    if(Trigger.isBefore && Trigger.isDelete){
        
        for(Custom__c c : Trigger.old){
            
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
    
    String[] toAddresses = new String[] {'test@gmail.com'};
	mail.setToAddresses(toAddresses); 
    mail.setSenderDisplayName('GWSCRM'); 
    mail.setUseSignature(false); 
    mail.setBccSender(false); 
    mail.setSaveAsActivity(false); 
    
   mail.setTreatTargetObjectAsRecipient(true);
   mail.setTargetObjectId(c.Id);
    //mail.setTreatBodiesAsTemplate(true);
   // mail.setWhatId(c.Id);
    EmailTemplate et=[Select id from EmailTemplate where Name=:'Canadian Project Management Delete Record']; 
    mail.setTemplateId(et.id); 
    
    
    Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); 

            
        }
    }


    
 
}
Error : Validation Errors While Saving Record(s)
There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger DeleteNotify caused an unexpected exception, contact your administrator: DeleteNotify: execution of BeforeDelete caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_TYPE_FOR_OPERATION, Only User, Contact, Lead, or Person objects are allowed for targetObjectId: a0D4F000000PczF.: [targetObjectId, a0D4F000000PczFUAS]: Trigger.DeleteNotify: line 24, column 1". 

Note : Using Email Template (Merge fileds)
Please let me know any one...

Regards,
Lakshmi.

 
sowmya Inturi 9sowmya Inturi 9
Hi Lakshmi,

You are giving the Custom__c  object Id in setTargetObjectId which is not possible as setTargetObjectId only accepts ID of the contact, lead, or user to which the email will be sent. 

There is no need of giving setTargetObjectId in Line no. 17 as you are already giving the setToAddresses in Line no. 10.

For further clarification refer to the below link.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_outbound_single.htm


Thanks,
Sowmya.
 
Lakshmi SLakshmi S
Hi sowmya,

After removing the ' setTargetObjectId '  i am getting below error, record not deleted.
Error : There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger DeleteNotify caused an unexpected exception, contact your administrator: DeleteNotify: execution of BeforeDelete caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Missing targetObjectId with template: []: Trigger.DeleteNotify: line 21, column 1". 

I want to show the deleted record details in mail, Please let me know.

Thanks,
Lakshmi.
 
sowmya Inturi 9sowmya Inturi 9
Hi Lakshmi,
As you are using the template it is needed.
Please specify to whom you want to send the email... if it is the record owner or and any contact related to it?

Thanks,
Sowmya.
Lakshmi SLakshmi S
Hi Sowmya,

I have created email template with merge fileds, Need to send this email template for " toAddress" which i have mentioned in the list of emails.
Please let me know.

Thanks,
Lakshmi.
sowmya Inturi 9sowmya Inturi 9
Hi Lakshmi,
Please modify your code with the below two lines:

 mail.setTreatTargetObjectAsRecipient(false);
 mail.setTargetObjectId(c.OwnerId);


This only sends the mail to the given toAddress and wont send the mail to owner as setTreatTargetObjectAsRecipient is false.

Thanks,
Sowmya.
Lakshmi SLakshmi S
Hi Sowmya,

It's not working.


Thanks,
Lakshmi.
sowmya Inturi 9sowmya Inturi 9
Hi,
Are you getting any mail to the toAddress mail Id?

But it won't display the merge field values as it is a custom object (It will only display for the contact, lead, or user ).
If you still want the merge fields to get displayed in the mail, you can create the custom template in the code itself and use the custom object fields.



Thanks,
Sowmya.
Ajay K DubediAjay K Dubedi
Hi Lakshmi,

Below code can fulfill your requirements. Hope this will work for you.
 
trigger DeleteNotify on Custom__c (Before Delete) {  
    if(Trigger.isBefore && Trigger.isDelete){       
        for(Custom__c c : Trigger.old){
            
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage()
        email.setToAddresses(new String[] {'abc@gmail.com'});
        email.setSubject('They are try to Delete Canadian Project Management Alert');
        email.setPlainTextBody('This message is to alert you that the  Project Management named ' + c.Name + ' has been deleted.');
        emails.add(email);
    }
        Messaging.sendEmail(emails);
        for (Custom__c cc : Trigger.old) 
    {
        cc.addError('Unable to delete record!');
    }     
    }
    }    
Please mark this as best answer if this solves your problem.

Thank you
Ajay Dubedi
Lakshmi SLakshmi S
Hi Sowmya,

Thank you very much for your response.
I resolved this issue using contact id in setTargetObjectId. Now it's working fine for email template.


Thanks,
Lakshmi