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
tommytxtommytx 

Code Runs direct in the Developer console but errors when I try to make a class out of it..

When I try to run the below code directly in the develpoment console, all is well but if I try to make a class out of it and run as class I get the following error.  What have I left out in my declaration?

 

 

Error: line 1, column 1: Method does not exist or incorrect signature: EmailTest()


// public class EmailTest() {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'test@test.com'};
mail.setToAddresses(toAddresses);
mail.setSenderDisplayName('Tom Salesforce');
mail.setSubject('This is subject line');
mail.setPlainTextBody('This is the content of this email.');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
// }

When the comment is removed i try to run in the console with EmailTest(); and that is when i get the error.

 

 

tommytxtommytx
// public class EmailTest() {
OOPS! The extra parentheiss () should not be there.

// public class EmailTest {
Vinit_KumarVinit_Kumar

Tommy,

 

Try below code :-

 

 public class EmailTest {

 

public EmailTest(){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'test@test.com'};
mail.setToAddresses(toAddresses);
mail.setSenderDisplayName('Tom Salesforce');
mail.setSubject('This is subject line');
mail.setPlainTextBody('This is the content of this email.');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

 

}
// }

tommytxtommytx
Thanks for the suggestion, but still not working... since the top brace is not commented out on your example and the bottom one is we have an extra brace...

But I tried just this code alone and it failed..
public EmailTest(){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'test@test.com'};
mail.setToAddresses(toAddresses);
mail.setSenderDisplayName('Tom Salesforce');
mail.setSubject('This is subject line');
mail.setPlainTextBody('This is the content of this email.');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}


}

Ankit AroraAnkit Arora

1) You need to write your code in some method and that method will be in the class

2) You can't use sendEmail in constructor

3) So create one method and put your code in it and use that method, it will work

 

public class EmailTest() {

 

public PageReference sendMyEmail() {

 

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'test@test.com'};
mail.setToAddresses(toAddresses);
mail.setSenderDisplayName('Tom Salesforce');
mail.setSubject('This is subject line');
mail.setPlainTextBody('This is the content of this email.');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

}

}

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

 

Vinit_KumarVinit_Kumar

Goof to know Ankit :)

tommytxtommytx

I think I am almost there but not yet..

 

I copied this code into a class

********************************

public class EmailTest() {
public PageReference sendMyEmail() {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'test@test.com'};
mail.setToAddresses(toAddresses);
mail.setSenderDisplayName('Tom Salesforce');
mail.setSubject('This is subject line');
mail.setPlainTextBody('This is the content of this email.');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}

 

But got the following error:

Error: Compile Error: expecting left curly bracket, found '(' at line 1 column 22

 

Here is what i did step by step..

Click Name - Setup - Deploy - Apex Classes - New

then cut and paste the above code and click Save and go the error above about left curly bracket.

 

Can someone follow my steps and tell me what I am overlooking?  Thanks

 

 

Ankit AroraAnkit Arora

It's my bad.

 

Please change the first line from 

 

public class EmailTest() {

 

to

 

public class EmailTest {

 

It will work ;)

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

tommytxtommytx

Do I need any of this stuff included in my class to let it know where all the SingleEmailMessage stuff is located?

 

Messaging.InboundEmail email  = new Messaging.InboundEmail();      
Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();            

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

 

as it seens to run fine as straight code but as soon as I encapsulate it in a class it either won't save or will not run.

Has anyone actually tried to dump my code into a class and try to run it.... If so I must be doing something totally wrong.