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
Dan Carney 6Dan Carney 6 

Get Started With Apex Exercise Problems

Hi. I'm having trouble with one of the exercises in the Get Started with Apex Triggers module, where you call a Class Method from a Trigger.

Here is the trigger I created. 


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('dtcarney08@gmail.com', 'Trailhead Trigger Tutorial', 
                              recordCount + ' contact(s) were inserted.');
    }
    else if (Trigger.isDelete) {
        // Process after delete
    }
}

This calls on this public class called EmailManager

public with sharing class EmailManager{
    public static void sendMail(String[] addresses, String[] subjects, String[] messages) {
        Messaging.SingleEmailMessage [] emails = new Messaging.SingleEmailMessage[]{};
        Integer totalMails = addresses.size();
        for(Integer i=0; i < totalMails; i++){
            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
            email.setSubject(subjects[i]);
            email.setToAddresses(new List<String> { addresses[i] });
            email.setPlainTextBody(messages[i]);
            emails.add(email);
        }
        Messaging.sendEmail(emails);
    }
}

However, there is a problem that shows up in the Developer Console that says “Method does not exist or incorrect signature: void sendMail (String, String, String) from the type EmailManager. I thought that I made the method static and I'm not sure what's wrong now. Any ideas of what's wrong?
Raj VakatiRaj Vakati
I got the reason ...Change your code as below .. your methond args are list of strings 
 
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(new String[]{'dtcarney08@gmail.com'}, new String[]{'Trailhead Trigger Tutorial'}, 
                             new String[]{ recordCount + ' contact(s) were inserted.'});
    }
    else if (Trigger.isDelete) {
        // Process after delete
    }
}

public with sharing class EmailManager{
    public static void sendMail(String[] addresses, String[] subjects, String[] messages) {
        Messaging.SingleEmailMessage [] emails = new Messaging.SingleEmailMessage[]{};
        Integer totalMails = addresses.size();
        for(Integer i=0; i < totalMails; i++){
            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
            email.setSubject(subjects[i]);
            email.setToAddresses(new List<String> { addresses[i] });
            email.setPlainTextBody(messages[i]);
            emails.add(email);
        }
        Messaging.sendEmail(emails);
    }
}


 
Sitanshu TripathiSitanshu Tripathi
Hi Dan,

Your code is I think right. Are you testing it with "Anonymus Window" of Developer Console. If Yes then write here. So that we can discuss.