You need to sign in to do that
Don't have an account?
Apex Trigger: Method does not exist or incorrect signature: MailManager.SendMail(String, String, String)
Hi,
I get that error when I try to save this code.
trigger ExampleTrigger2 on Contact (after insert, after delete) {
if (Trigger.isInsert) {
Integer recordCount = Trigger.New.size();
// Call a utility method from another class
EmailManager.sendMail('aschor@acmeunited.com', 'Trailhead Trigger Tutorial',
recordCount + ' contact(s) were inserted.');
}
else if (Trigger.isDelete) {
// Process after delete
}
}
thanks,
Aron
Please let us know if this will help you
All Answers
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;
}
}
sendMail() has to be declared as static ..try this one
public static sendMail(String address, String subject, String body)
public static sendMail(String address, String subject, String body) {
from
public void sendMail(String address, String subject, String body) {
It says
Error Error: Compile Error: Constructors cannot be static at line 4 column 19
Please let us know if this will help you
Can you please elaborate why do we need to change the method to static.
Thanks in advance.
what if i change to
then there is no need of writing this below line and directly i can write
but on testing it is showing error. Is function signatur of " sendMail" is of this type
by default in apex
Thanks!
EmailManager emailManger = new EmailManager();
emailManger.sendMail('mail id', 'Trailhead Trigger Tutorial',
recordCount + ' contact(s) were inserted.');
While copying the code from trailhead to developer console, the blank lines get numbered automatically. I am extremely new to coding, so I not sure why they showed up automarically or if there is a way to override/ignore those.
But after I removed those numbers, the code ran fine and the email was sent. Did not have to make any changes to public void declarations.
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ExampleTrigger: execution of AfterInsert caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_ID_FIELD, ID is invalid or you do not have access to the record.: [toAddresses, Your email address] Class.EmailManager1.sendMail: line 12, column 1 Trigger.ExampleTrigger: line 5, column 1: []
System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_ID_FIELD, ID is invalid or you do not have access to the record.: [toAddresses, Your email address]
I have to type my Email Address instead of 'Your email address' in Execute Anonymous Window .
Make sure you save your code before you run it and make this line of code static. It's missing static from what is given in trailheads.
"Public static void sendMail(String address, String subject, String body)" Also make sure to update the line "your email address" while executing to your own email address and that should work. If not lmk
I am getting below error while compiling:
Static method cannot be referenced from a non static context: void EmailManager.sendMail(String, String, String)
EmailManager.sendMail(addresses, subjects, messages);
Comment or delete the following code because it is instantiating the EmailManage class.
EmailManager em = new EmailManager();
em.sendMail(addresses, subjects, messages);