• Sarah Beutel
  • NEWBIE
  • 5 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    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
 

Does anyone know how to get the value of the Account Team Members to the Account object?  My issue is that I have a custom object with a master-detail relationship to the Account object.  On my custom object, I would like to list one of the Account Team members if they are a certain role.  The only way I can see to do this would be to have the value of the Account Team on the Account level either as a text field or a lookup field.  I don't need it to be a lookup field on my custom object, just the first and last names.  Can anyone help or at least let me know if it's possible?  Thanks

  • September 25, 2012
  • Like
  • 0