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
Reddivari ManjunathReddivari Manjunath 

ExampleTrigger in trailhead practice

Hi,
I am new salesforce and struggling with triggers.Now i am try to execute the sample trigger which was send mail  to mentioned email with description.below the code which i am working now.its executed successfully but not triggered the mail to mentioned mail.

trigger ExampleTrigger on Contact (after insert, after delete) {
     if (Trigger.isInsert) {
        Integer recordCount = Trigger.New.size();
         System.debug(':::countrecords:::'+recordCount);
        // Call a utility method from another class
        EmailManager.sendMail('emailtomanjur@gmail.com', 'Trailhead Trigger Tutorial', 
                    recordCount + ' contact(s) were inserted.');
    }
    else if (Trigger.isDelete) {
        // Process after delete
    }

}
Best Answer chosen by Reddivari Manjunath
sfdcMonkey.comsfdcMonkey.com
hi Reddivari Manjunath,
have you create the 'EmailManager' apex class? if not create apex class from here (https://trailhead.salesforce.com/en/modules/apex_database/units/apex_database_intro):
after that tell me the steps how you validate this trigger
thanks

All Answers

sfdcMonkey.comsfdcMonkey.com
hi Reddivari Manjunath,
have you create the 'EmailManager' apex class? if not create apex class from here (https://trailhead.salesforce.com/en/modules/apex_database/units/apex_database_intro):
after that tell me the steps how you validate this trigger
thanks
This was selected as the best answer
Amit Chaudhary 8Amit Chaudhary 8

If your Salesforce Administrator has configured the Email Administration Deliverability in your org with No Access then it restricts emails from your org ( Production or sandbox) and to resolve this one of your Salesforce Administrators needs to change the Deliverability to All Emails

Let us know if this will help you

I hope your EmailManager class is like below
public class EmailManager {

    // Public method
    public void sendMail(String address, String subject, String body) {
        // Create an email message object
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {address};
        mail.setToAddresses(toAddresses);
        mail.setSubject(subject);
        mail.setPlainTextBody(body);
        // Pass this email message to the built-in sendEmail method 
        // of the Messaging class
        Messaging.SendEmailResult[] results = Messaging.sendEmail(
                                 new Messaging.SingleEmailMessage[] { mail });
        
        // Call a helper method to inspect the returned results
        inspectResults(results);
    }
    
    // Helper method
    private static Boolean inspectResults(Messaging.SendEmailResult[] results) {
        Boolean sendResult = true;
        
        // sendEmail returns an array of result objects.
        // Iterate through the list to inspect results. 
        // In this class, the methods send only one email, 
        // so we should have only one result.
        for (Messaging.SendEmailResult res : results) {
            if (res.isSuccess()) {
                System.debug('Email sent successfully');
            }
            else {
                sendResult = false;
                System.debug('The following errors occurred: ' + res.getErrors());                 
            }
        }
        
        return sendResult;
    }

}

 
Reddivari ManjunathReddivari Manjunath
Hi Piyush,Amit,
Yes i created the 'EmailManager' at first only.After that i run through Debug anonymous window with following code.its successfully executed but i didnt get any mail with mentioned message and also set the email seetings also.

Contact c = new Contact(LastName='Test Contact');
insert c;
Reddivari ManjunathReddivari Manjunath
Hi Piyush,
  Thanks for your valuable solution.its executing but not triggering mail and in debug log nothing will be showing.in trigger showing some compile error while calling the sendMail method.below the error message.

"Non static method cannot be referenced from a static context: void EmailManager.sendMail(String, String, String)"
sfdcMonkey.comsfdcMonkey.com
that is why because your apex class method sendMail() is non static, please copy below class and override your existing class code with it
public class EmailManager {

    // Public method
    public static void sendMail(String address, String subject, String body) {
        // Create an email message object
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {address};
        mail.setToAddresses(toAddresses);
        mail.setSubject(subject);
        mail.setPlainTextBody(body);
        // Pass this email message to the built-in sendEmail method 
        // of the Messaging class
        Messaging.SendEmailResult[] results = Messaging.sendEmail(
                                 new Messaging.SingleEmailMessage[] { mail });
        
        // Call a helper method to inspect the returned results
        inspectResults(results);
    }
    
    // Helper method
    private static Boolean inspectResults(Messaging.SendEmailResult[] results) {
        Boolean sendResult = true;
        
        // sendEmail returns an array of result objects.
        // Iterate through the list to inspect results. 
        // In this class, the methods send only one email, 
        // so we should have only one result.
        for (Messaging.SendEmailResult res : results) {
            if (res.isSuccess()) {
                System.debug('Email sent successfully');
            }
            else {
                sendResult = false;
                System.debug('The following errors occurred: ' + res.getErrors());                 
            }
        }
        
        return sendResult;
    }

}
Thanks
Reddivari ManjunathReddivari Manjunath
Thanks Piyush,
    got executed.Thanks once again for your valuable solution.

Regards,
Manjunath
Canio A. CaputoCanio A. Caputo
Thanks Piyush, 
that worked for me too!