• anthony curtis 18
  • NEWBIE
  • 5 Points
  • Member since 2018


  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies
In the unit 'Get Started with Apex Triggers' there is an exercise in the section titled 'Calling a Class Method from a Trigger'.

Having done the following:-
Checked that the EmailManager class was in my org and checked that the sendMail() method therein was set to static.
Copied and pasted the trigger code in to the ExampleTrigger apex trigger, changed the email address to my own email address and saved the trigger.
In the Execute Anonymous Window I then created an account, as instructed.

The debug log suggests that the trigger fired but there is no indication therein that the email was sent.

So, not suprisingly, When I check my inbox (and Junk box) I note that the email has not arrived.

The contact record is created successfully in my org.

Why is the static EmailManager.sendMail method not being invoked within the trigger? Please help.

Here is the trigger code (with my email address obfuscated):-

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

Here is the EmailManager class definition:-
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;
    }

}

Here is the execution log:-

41.0 APEX_CODE,FINEST;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WAVE,INFO;WORKFLOW,INFO
Execute Anonymous: Contact c = new Contact(LastName='Test Contact');
Execute Anonymous: insert c;
12:43:36.1 (1043843)|USER_INFO|[EXTERNAL]|0050N000007SRGO|anthonyjcurtis@curious-badger-378343.com|Greenwich Mean Time|GMTZ
12:43:36.1 (1064614)|EXECUTION_STARTED
12:43:36.1 (1068624)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
12:43:36.1 (1182833)|VARIABLE_SCOPE_BEGIN|[1]|c|Contact|true|false
12:43:36.1 (1302463)|HEAP_ALLOCATE|[72]|Bytes:3
12:43:36.1 (1339570)|HEAP_ALLOCATE|[77]|Bytes:152
12:43:36.1 (1354302)|HEAP_ALLOCATE|[342]|Bytes:408
12:43:36.1 (1369051)|HEAP_ALLOCATE|[355]|Bytes:408
12:43:36.1 (1382129)|HEAP_ALLOCATE|[467]|Bytes:48
12:43:36.1 (1408829)|HEAP_ALLOCATE|[139]|Bytes:6
12:43:36.1 (1424721)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:3
12:43:36.1 (1507219)|STATEMENT_EXECUTE|[1]
12:43:36.1 (1510286)|STATEMENT_EXECUTE|[1]
12:43:36.1 (1538084)|HEAP_ALLOCATE|[1]|Bytes:4
12:43:36.1 (1617345)|HEAP_ALLOCATE|[1]|Bytes:12
12:43:36.1 (1668374)|VARIABLE_ASSIGNMENT|[1]|this.LastName|"Test Contact"|0x5bcdb109
12:43:36.1 (1706864)|VARIABLE_ASSIGNMENT|[1]|c|{"LastName":"Test Contact"}|0x5bcdb109
12:43:36.1 (1713832)|STATEMENT_EXECUTE|[2]
12:43:36.1 (1749458)|HEAP_ALLOCATE|[50]|Bytes:5
12:43:36.1 (1768033)|HEAP_ALLOCATE|[56]|Bytes:5
12:43:36.1 (1775095)|HEAP_ALLOCATE|[64]|Bytes:7
12:43:36.1 (1809125)|HEAP_ALLOCATE|[2]|Bytes:8
12:43:36.1 (1817712)|DML_BEGIN|[2]|Op:Insert|Type:Contact|Rows:1
12:43:36.1 (1846351)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
12:43:36.1 (40147057)|CODE_UNIT_STARTED|[EXTERNAL]|01q0N000000eR1D|ExampleTrigger on Contact trigger event BeforeInsert for [new]
12:43:36.1 (40200510)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
12:43:36.1 (40312758)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4
12:43:36.1 (40329094)|VARIABLE_SCOPE_BEGIN|[1]|this|ExampleTrigger|true|false
12:43:36.1 (40375261)|VARIABLE_ASSIGNMENT|[1]|this|{}|0x768fab51
12:43:36.1 (40419799)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4
12:43:36.1 (40428625)|VARIABLE_SCOPE_BEGIN|[1]|this|ExampleTrigger|true|false
12:43:36.1 (40443016)|VARIABLE_ASSIGNMENT|[1]|this|{}|0x768fab51
12:43:36.1 (40454037)|STATEMENT_EXECUTE|[1]
12:43:36.40 (40458500)|CUMULATIVE_LIMIT_USAGE
12:43:36.40 (40458500)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 1 out of 150
  Number of DML rows: 1 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

12:43:36.40 (40458500)|CUMULATIVE_LIMIT_USAGE_END

12:43:36.1 (41147270)|CODE_UNIT_FINISHED|ExampleTrigger on Contact trigger event BeforeInsert for [new]
12:43:36.1 (76356758)|DML_END|[2]
12:43:36.93 (93180944)|CUMULATIVE_LIMIT_USAGE
12:43:36.93 (93180944)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 1 out of 150
  Number of DML rows: 1 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

12:43:36.93 (93180944)|CUMULATIVE_LIMIT_USAGE_END

12:43:36.1 (93222152)|CODE_UNIT_FINISHED|execute_anonymous_apex
12:43:36.1 (94380801)|EXECUTION_FINISHED
 
In the unit 'Get Started with Apex Triggers' there is an exercise in the section titled 'Calling a Class Method from a Trigger'.

Having done the following:-
Checked that the EmailManager class was in my org and checked that the sendMail() method therein was set to static.
Copied and pasted the trigger code in to the ExampleTrigger apex trigger, changed the email address to my own email address and saved the trigger.
In the Execute Anonymous Window I then created an account, as instructed.

The debug log suggests that the trigger fired but there is no indication therein that the email was sent.

So, not suprisingly, When I check my inbox (and Junk box) I note that the email has not arrived.

The contact record is created successfully in my org.

Why is the static EmailManager.sendMail method not being invoked within the trigger? Please help.

Here is the trigger code (with my email address obfuscated):-

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

Here is the EmailManager class definition:-
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;
    }

}

Here is the execution log:-

41.0 APEX_CODE,FINEST;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WAVE,INFO;WORKFLOW,INFO
Execute Anonymous: Contact c = new Contact(LastName='Test Contact');
Execute Anonymous: insert c;
12:43:36.1 (1043843)|USER_INFO|[EXTERNAL]|0050N000007SRGO|anthonyjcurtis@curious-badger-378343.com|Greenwich Mean Time|GMTZ
12:43:36.1 (1064614)|EXECUTION_STARTED
12:43:36.1 (1068624)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
12:43:36.1 (1182833)|VARIABLE_SCOPE_BEGIN|[1]|c|Contact|true|false
12:43:36.1 (1302463)|HEAP_ALLOCATE|[72]|Bytes:3
12:43:36.1 (1339570)|HEAP_ALLOCATE|[77]|Bytes:152
12:43:36.1 (1354302)|HEAP_ALLOCATE|[342]|Bytes:408
12:43:36.1 (1369051)|HEAP_ALLOCATE|[355]|Bytes:408
12:43:36.1 (1382129)|HEAP_ALLOCATE|[467]|Bytes:48
12:43:36.1 (1408829)|HEAP_ALLOCATE|[139]|Bytes:6
12:43:36.1 (1424721)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:3
12:43:36.1 (1507219)|STATEMENT_EXECUTE|[1]
12:43:36.1 (1510286)|STATEMENT_EXECUTE|[1]
12:43:36.1 (1538084)|HEAP_ALLOCATE|[1]|Bytes:4
12:43:36.1 (1617345)|HEAP_ALLOCATE|[1]|Bytes:12
12:43:36.1 (1668374)|VARIABLE_ASSIGNMENT|[1]|this.LastName|"Test Contact"|0x5bcdb109
12:43:36.1 (1706864)|VARIABLE_ASSIGNMENT|[1]|c|{"LastName":"Test Contact"}|0x5bcdb109
12:43:36.1 (1713832)|STATEMENT_EXECUTE|[2]
12:43:36.1 (1749458)|HEAP_ALLOCATE|[50]|Bytes:5
12:43:36.1 (1768033)|HEAP_ALLOCATE|[56]|Bytes:5
12:43:36.1 (1775095)|HEAP_ALLOCATE|[64]|Bytes:7
12:43:36.1 (1809125)|HEAP_ALLOCATE|[2]|Bytes:8
12:43:36.1 (1817712)|DML_BEGIN|[2]|Op:Insert|Type:Contact|Rows:1
12:43:36.1 (1846351)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
12:43:36.1 (40147057)|CODE_UNIT_STARTED|[EXTERNAL]|01q0N000000eR1D|ExampleTrigger on Contact trigger event BeforeInsert for [new]
12:43:36.1 (40200510)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
12:43:36.1 (40312758)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4
12:43:36.1 (40329094)|VARIABLE_SCOPE_BEGIN|[1]|this|ExampleTrigger|true|false
12:43:36.1 (40375261)|VARIABLE_ASSIGNMENT|[1]|this|{}|0x768fab51
12:43:36.1 (40419799)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4
12:43:36.1 (40428625)|VARIABLE_SCOPE_BEGIN|[1]|this|ExampleTrigger|true|false
12:43:36.1 (40443016)|VARIABLE_ASSIGNMENT|[1]|this|{}|0x768fab51
12:43:36.1 (40454037)|STATEMENT_EXECUTE|[1]
12:43:36.40 (40458500)|CUMULATIVE_LIMIT_USAGE
12:43:36.40 (40458500)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 1 out of 150
  Number of DML rows: 1 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

12:43:36.40 (40458500)|CUMULATIVE_LIMIT_USAGE_END

12:43:36.1 (41147270)|CODE_UNIT_FINISHED|ExampleTrigger on Contact trigger event BeforeInsert for [new]
12:43:36.1 (76356758)|DML_END|[2]
12:43:36.93 (93180944)|CUMULATIVE_LIMIT_USAGE
12:43:36.93 (93180944)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 1 out of 150
  Number of DML rows: 1 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

12:43:36.93 (93180944)|CUMULATIVE_LIMIT_USAGE_END

12:43:36.1 (93222152)|CODE_UNIT_FINISHED|execute_anonymous_apex
12:43:36.1 (94380801)|EXECUTION_FINISHED
 
In the 'Getting Started with APEX Triggers' (http://goo.gl/5YJ01q) module, an error message appears when attempting the first example/exercise. 
 The instructions read: 
  • 1. In the Developer Console, click File | New | Trigger. (The menu path is actually File | New | APEX Trigger).
  • 2. Enter helloWorldTrigger for the trigger name, and then select Account for the sObject. ClickSubmit. (stet)
When clicking submit in step 2, an error message appears: "The Trigger "HelloWorldTrigger " is not a legal name." The parenthesis around HelloWorldTrigger are not entered in step 2. 

Work Around:  
In Setup go Develop | Apex Triggers
Launch the Dev Console (DC) from setup UI. 
Removed previous text from within the Query Editor tab which contained an invalid query, and was promtping an error message itself.
Continued with set 2, above. 

I was able to save the new trigger in the Dev Console. I then deleted it, closed the DC, and repeated the steps from the Trailhead exercise (where the DC was launched from Name | Dev Console, not Setup | Develop). I was then able to save the trigger without error. 

Take Away: it may have been the erronous text in the query editor, when the DC was launched from Name | DC. Subsequently adding erronous text back into the query editor, and repeating set 2 above did not throw a naming error the second time.