0

I am trying to call an apex method on an apex trigger. But here i am receiving following error. Here is the code which i used.

Apex Class:

public class EmailExample {

    public void mailSending(){
        Contact con= new Contact();
        String mail1=con.email;
        String[] mail2= new String[]{mail1};  
        Messaging.SingleEmailMessage email= new Messaging.SingleEmailMessage();
        String[] toadd= new String[]{'mail'}; 
        email.setToAddresses(mail2);
        Messaging.Email[] mail3 = new Messaging.Email[]{email};
        Messaging.sendEmail(mail3);
    }
}

Apex Trigger:

trigger MailingContactTrigger on Contact (before insert) {
    EmailExample.mailSending();

}

Error which i am getting:

Method does not exist or incorrect signature: EmailExample.mailSending()

could you please anyone clarify why i am getting above error.

Thanking you KS Kumar

4

your method should be static

public static void mailSending(){
//rest of your code
}

now you can call this method using

EmailExample.mailSending();

if you don't want to use static method then you could do like below example

public class EmailExample {

    public void mailSending(){
        //rest of your code 
    }
}

and in trigger

trigger MailingContactTrigger on Contact (before insert) {
    EmailExample emailHandler = new EmailExample();
    emailHandler.mailSending();

}

first initialize and then call your class method

| improve this answer | |
3

The issue is that the method has been defined as an instance method, meaning you cannot access it directly, you first must instantiate the class.

For example, you would have to do the following to call the method:

new EmailExample().mailSending();

Now if you don't want this you could alternatively define the method as static. Static methods do not require you to instantiate the class and can be accessed directly.

To define a method as static add the modifier "static" to the method signature:

public static void mailSending(){
  // your code
}

Bare in mind that you cannot access properties or methods which are not marked as static within your mailSending method.

Hope this helps!

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.