0
public with sharing class EmailNotification {
   /**
   * 
   * @param  List<SObject> record
   * @param  String errorMsg 
   */
   public sendEmailAlert(List<sObject> record, String errorMsg){

      Messaging.SingleEmailMessage mail =  new Messaging.SingleEmailMessage();

      String[] toAddresses = new List<String>{
         UserInfo.getUserEmail()         
      };

      mail.setToAddresses(toAddresses);
      mail.setSubject( '' + Datetime.now().format() );

      String message = '' + '\n' ;
      message = message + '' + errorMessage;

      mail.setPlainTextBody(message);
      List<Messaging.SendEmailResult> results = Messaging.sendEmail(
         new List<Messaging.SingleEmailMessage>{mail},
         false
      );

      for (Messaging.SendEmailResult lvresult : results) {
         if (lvresult.isSuccess() == false) {
            System.debug(LoggingLevel.WARN,
                         'Erreur envoi email : ' +
                         lvresult.getErrors()[0].getMessage()
            );
         }
      }
   }
}

I want to call my method from other class, in a catch exception.. When DML fails, I want to receive a mail alert.

9
  • 2
    Welcome to Salesforce Stack Exchange (SFSE). Thank you for including code, but there is no actual question in your post. Please take the Tour & look in the Help Center - especially What topics can I ask about here? where it states, '...a question post should include 1) A specific, detailed description of the issue ("it doesn’t work" is not enough to allow the community to help). plus ... 3) and/or the research and work you’ve done so far on the issue.' Please edit your question to add info.
    – Moonpie
    Aug 31 at 15:14
  • I would like to use "sendEmailAlert" method in different class using try-catch every time a DML statement is present. How can I re-built my class to accomplish that?
    – WildMan
    Aug 31 at 15:16
  • Firstly, please edit your question (on the bottom left of your question are the words "Share Edit Follow Flag" - click on "Edit") to add additional pertinent information, rather than adding in the comments where it might be overlooked.
    – Moonpie
    Aug 31 at 15:22
  • Secondly, when you say, '...use "sendEmailAlert" method in a different class...' do you mean that you want to A) call this method from other classes, or B) move this method to another class? (Or something else?) And when you say, '..using try-catch every time a DML statement is present...' do you mean that you want to A) add try...catch to this method, or B) add try...catch to the other class?
    – Moonpie
    Aug 31 at 15:31
  • I want to call my method from other class, in a catch exception.. When DML fails, I want to receive a mail alert.
    – WildMan
    Aug 31 at 15:43

Your Answer

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

Browse other questions tagged or ask your own question.