0

The use case I am trying to satisfy is to create a notification when an opportunity is deleted. From my investigation, I believe I cannot do this out of the box using a workflow.

So I created a simple trigger to fire when an opp was deleted – I can get it to fire an email notification but was really wanting it to generate an out bound message (similar to that of a workflow action) where I can link to an Endpoint URL.

I thought this should be possible but after searching about I couldn’t get anything to work – is this possible and if so some pointers would be appreciated. My workaround was to create a custom table, get the trigger to fire on delete and insert a record into this table. I then create a workflow/endpoint URL on insert into this table. It’s a bit clunky and was looking for a slicker solution.

Here is the code for my trigger with email notification:

trigger NotifyOppDelete 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[] {'dave@testmail.com'});
        email.setSubject('Deleted Opportunity Alert');
        email.setPlainTextBody('This message is to alert you that the Opportunity named ' + opp.Name + ' has been deleted.');
        emails.add(email);     
    }
    Messaging.sendEmail(emails);
}
  • I think you're on the right track here. If you really need to use an outbound message then your approach seems fine. Just keep in mind to add logic to delete your temporary record. I probably would go down making a call out to your endpoint from the trigger but that's your call... – Jannis Bott Apr 17 '17 at 9:38
  • Thanks Jannis - making the callout to the endpoint from the trigger would be my preferred option too but I was struggling with the syntax... can you direct me to a good tutorial/example? – DaveS Apr 17 '17 at 11:09
  • Trailhead is a good option here. They have a tutorial on sending a SOAP message: trailhead.salesforce.com/en/modules/apex_integration_services/… – Jannis Bott Apr 17 '17 at 11:12
0

DaveS, there are few changes I will suggest you do, but I think the first one and most important will be to move from "After" to "Before", then you can try something on these lines:

trigger NotifyOppDelete on Opportunity(before delete, before insert, before update, 
                                after delete, after insert, after update) {

if(Trigger.isBefore)
{
 if(Trigger.isDelete)
  {
    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[] {'dave@testmail.com'});
    email.setSubject('Deleted Opportunity Alert');
    email.setPlainTextBody('This message is to alert you that the Opportunity named ' + opp.Name + ' has been deleted.');
    emails.add(email);     
 }
       Messaging.sendEmail(emails);
  }
 }
}

I tested this and it works, just make sure that you check your Junk.

This is ok but it is always more efficient to move any logic out of your triggers. The point is that the triggers should contain as less code as possible and act more as a bridge to other classes like Helper Classes or Trigger Handlers.

I have some tips on my site here, still on construction.

So you could do something like this to move your logic out of your trigger body:

trigger TriggerOnOpportunity on Opportunity(before delete, before insert, before update, 
                                after delete, after insert, after update) {

if(Trigger.isBefore)
{
 if(Trigger.isDelete)
  {
    BeforeDeletingOpportunities.beforeDeleteOperations();
  }
 }
}

On that trigger, you can handle all Trigger Context Variables for the Opportunity object.

The line: BeforeDeletingOpportunities.beforeDeleteOperations(); is invoking a class and a method in that class that will take care of the DML operation, in this case, a "Before Delete".

This is the code for that class:

public with sharing class BeforeDeletingOpportunities
{

  public static void beforeDeleteOperations()
  {
    Messaging.reserveSingleEmailCapacity(trigger.size);
    List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();

  for(Opportunity opp: (List<Opportunity>) trigger.old)
  {
     Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
     email.setToAddresses(new String[] {'dave@testmail.com'});
     email.setSubject('Deleted Opportunity Alert');
     email.setPlainTextBody('This message is to alert you that the Opportunity named ' + opp.Name + ' has been deleted.');
     emails.add(email);     
   }
      Messaging.sendEmail(emails);

   }

 }

This line: for(Opportunity opp: (List<Opportunity>) trigger.old), is important.

Like you can see is a small effort to get more control over your triggers. Give it a try if you can.

Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.