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
Inbox OutboxInbox Outbox 

Greetings! Code help please.

I am trying to send a mass welcome email (new customer template) to contacts after we save the contact. All I want is to know why I am getting the error and the solution for that. 
I have the following code with an error. Please advice!

class:
public class massEmail 

{
    public void mail(){
        list<contact> l= [SELECT ID FROM contact LIMIT 2];
        list<ID> I= new list<ID>();
        for(contact c: l){
            I.add(C.Id);    
        }
        
        EmailTemplate e = [SELECT ID FROM EmailTemplate WHERE Name = 'Sales: New Customer Email'];
        messaging.MassEmailMessage  m = new messaging.MassEmailMessage();
        m.setTargetObjectIds(I);
        m.setSenderDisplayName('System Admin');
        m.setTemplateId(e.Id);
        messaging.sendEmail(new messaging.MassEmailMessage [] {m});     
    }
}

Trigger:
trigger NewCustEmail on Contact (after insert)

{
    if(trigger.IsAfter){
         massEmail.mail(trigger.new);
    }
}

Error: 
Method does not exist or incorrect signature: void email(List<Contact>) from the type massEmail
Best Answer chosen by Inbox Outbox
Suraj Tripathi 47Suraj Tripathi 47

Hi,

Yeah, I undestood

1. You need to make your method static like 

public static void mail(List<Contact> conList){

2.You are not passing the argument in your method "mail()" but you are sending an argument from trigger "massEmail.mail(trigger.new);" . It is mandatory to pass argument in the method.

public static void mail(List<Contact> conList){}
 

so you can correct your whole code like this:

public class massEmail {
    public static void mail(List<Contact> conList){
        list<contact> l= [SELECT ID FROM contact where id in:  conList];
        list<ID> I= new list<ID>();
        for(contact c: l){
            I.add(C.Id);    
        }
        EmailTemplate e = [SELECT ID FROM EmailTemplate WHERE Name = 'Sales: New Customer Email'];
        messaging.MassEmailMessage  m = new messaging.MassEmailMessage();
        m.setTargetObjectIds(I);
        m.setSenderDisplayName('System Admin');
        m.setTemplateId(e.Id);
        messaging.sendEmail(new messaging.MassEmailMessage [] {m});     
    }
}

if your query is solved please mark it as Best Answer

Thank You

All Answers

PriyaPriya (Salesforce Developers) 

Hi 

You are getting the error because your method is not accepting the parametre in apex class but you are passing parameter in trigger and hence the error.

Try updating the method as below :- 

public class massEmail 

{
    public void mail(List<Contact> newList){
        list<ID> I= new list<ID>();
        
        for(contact c: newList){
            I.add(c.Id);    
        }
        
        EmailTemplate e = [SELECT ID FROM EmailTemplate WHERE Name = 'Sales: New Customer Email'];
        messaging.MassEmailMessage  m = new messaging.MassEmailMessage();
        m.setTargetObjectIds(I);
        m.setSenderDisplayName('System Admin');
        m.setTemplateId(e.Id);
        messaging.sendEmail(new messaging.MassEmailMessage [] {m});     
    }
}

 

Please mark it as Best Answer so that it can help others in the future.

Regards,

Priya Ranjan

 

Inbox OutboxInbox Outbox
Let me try that. Thank you for replying Priya. 
Suraj Tripathi 47Suraj Tripathi 47
Hi Inbox,

You are not provided the method to static first you have to provide that method to static.I provided you the reference link for your convenience.

https://developer.salesforce.com/forums/?id=9060G000000Xa5DQAS

https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_email_outbound_mass.htm

If you find your answer mark as the best answer,
Thanks and Regards,
Suraj Tripathi.
Suraj Tripathi 47Suraj Tripathi 47

Hi,

Yeah, I undestood

1. You need to make your method static like 

public static void mail(List<Contact> conList){

2.You are not passing the argument in your method "mail()" but you are sending an argument from trigger "massEmail.mail(trigger.new);" . It is mandatory to pass argument in the method.

public static void mail(List<Contact> conList){}
 

so you can correct your whole code like this:

public class massEmail {
    public static void mail(List<Contact> conList){
        list<contact> l= [SELECT ID FROM contact where id in:  conList];
        list<ID> I= new list<ID>();
        for(contact c: l){
            I.add(C.Id);    
        }
        EmailTemplate e = [SELECT ID FROM EmailTemplate WHERE Name = 'Sales: New Customer Email'];
        messaging.MassEmailMessage  m = new messaging.MassEmailMessage();
        m.setTargetObjectIds(I);
        m.setSenderDisplayName('System Admin');
        m.setTemplateId(e.Id);
        messaging.sendEmail(new messaging.MassEmailMessage [] {m});     
    }
}

if your query is solved please mark it as Best Answer

Thank You

This was selected as the best answer