+ Start a Discussion
Ben Olsen 37Ben Olsen 37 

delete trigger ( i'm new to coding)

im currently writing a trigger to send an email to the account owner, when a contact is deleted,  I am almost there,  can anyone point out where i am going wrong.  Also how do i write a test class for this?  Here is what i have so far I copied this and changed it to what i was needing,  but don't know how to reference the account owner email and where it goes,  is it owner.email?

trigger EmailAfterDelete on Contact (after delete) {
Messaging.reserveSingleEmailCapacity(trigger.size);
    List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
    for (Contact Contact : Trigger.old) {
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        email.setToAddresses(new String[] {'some email address'};);
        email.setSubject('Deleted Contact Alert');
        email.setPlainTextBody('This message is to alert you that the Contact named ' + Contact.Name +' on Account' + Account.Name +
                               ' has been deleted.');
        emails.add(email);
    }
    Messaging.sendEmail(emails);

}
Best Answer chosen by Ben Olsen 37
Uttpal_ChandraUttpal_Chandra
Hi Ben,

I have update your code little bit.
trigger EmailAfterDelete on Contact (after delete) 
{

    Account ab = [Select Name,Owner.Email from Account where id=: Trigger.old[0].accountid];
      String name = Trigger.old[0].FirstName + ' ' +  Trigger.old[0].LastName ;
    
    List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
    List<String> send = new List<String>();
    send.add(ab.Owner.Email);
    email.setToAddresses(send);
    email.setSubject('Deleted Contact Alert');
    email.setPlainTextBody('This message is to alert you that the Contact named ' + name +' on Account ' + ab.Name +
                           ' has been deleted.');
    emails.add(email);
    Messaging.sendEmail(emails);

}

If you move your code from sandbox to production you need to write test class.

Thanks and Regards,
Uttpal Chandra

All Answers

Uttpal_ChandraUttpal_Chandra
Hi Ben,

I have update your code little bit.
trigger EmailAfterDelete on Contact (after delete) 
{

    Account ab = [Select Name,Owner.Email from Account where id=: Trigger.old[0].accountid];
      String name = Trigger.old[0].FirstName + ' ' +  Trigger.old[0].LastName ;
    
    List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
    List<String> send = new List<String>();
    send.add(ab.Owner.Email);
    email.setToAddresses(send);
    email.setSubject('Deleted Contact Alert');
    email.setPlainTextBody('This message is to alert you that the Contact named ' + name +' on Account ' + ab.Name +
                           ' has been deleted.');
    emails.add(email);
    Messaging.sendEmail(emails);

}

If you move your code from sandbox to production you need to write test class.

Thanks and Regards,
Uttpal Chandra
This was selected as the best answer
Ben Olsen 37Ben Olsen 37
ok great,  in the test class, all i need to do is create a dummy account with a contact and then delete it?
Uttpal_ChandraUttpal_Chandra
Yes Ben