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
M AnM An 

Getting error Expecting '}' but was: 'for'

Scenario: When contact last name does not equal to null, send an email to that contact

public class sendemail_contacts 
{
list<contact>lstcont=new list<contact>();    
for(lstcont=[select id,name from contact where firstname!=''])
{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();  
string body = 'Hi ';
String[] toAddresses = new String[] {'example@gmail.com'}; 
mail.setToAddresses(toAddresses);
mail.setSubject('Test Subject'); 
}
}

I am new to Salesforce development. I dont know why this error has been occured.
Can you please anyone explain and suggest the answer for this below code?
 
Best Answer chosen by M An
NForceNForce
Hi M An,
There are couple changes you need to make to this class.
# Class doesn't have a single method, you did all in the class which is not a good practice. 
# In requirement you said  last name but in query you were filtering first name, change it to last name
# Use sendEmail method to send email. 

I updated the code, call the method sendemail_contacts .sendEmail()

public class sendemail_contacts 
{
public static void sendEmail(){  // create a method
list<contact>lstcont=new list<contact>();    
for(lstcont=[select id,name from contact where lastname!=''])// changed filter
{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();  
string body = 'Hi ';
String[] toAddresses = new String[] {'example@gmail.com'}; 
mail.setToAddresses(toAddresses);
mail.setSubject('Test Subject'); 
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); // sendEmail - this method sends email
}
}
}

Please make sure you bulkify the code before you send it to production.

Thanks

All Answers

v varaprasadv varaprasad

Hi,

Using workflow rule you can create email alert.
Steps  : 
1.Create email template
2.Create workflow rule criteria like whenever record is created.
3.Then Add email alert to workflow and activte the workflow.

Using trigger also we can send email.


More info : 
https://success.salesforce.com/answers?id=90630000000DJ37AAG


Thanks
Varaprasad




 

M AnM An

Hi, Varaprasad thank you for your quick reply.

I have tried this scenario in workflow and other configuration parts. I know the number of possibilities to send an email.But my question is to send email using apex class. So I tried the above code and I know it's not a correct implementation.So, can you please correct me to avoid the errors of this mentioned code.

 

M AnM An
can anyone please suggest why this error has occurred?

 
NForceNForce
Hi M An,
There are couple changes you need to make to this class.
# Class doesn't have a single method, you did all in the class which is not a good practice. 
# In requirement you said  last name but in query you were filtering first name, change it to last name
# Use sendEmail method to send email. 

I updated the code, call the method sendemail_contacts .sendEmail()

public class sendemail_contacts 
{
public static void sendEmail(){  // create a method
list<contact>lstcont=new list<contact>();    
for(lstcont=[select id,name from contact where lastname!=''])// changed filter
{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();  
string body = 'Hi ';
String[] toAddresses = new String[] {'example@gmail.com'}; 
mail.setToAddresses(toAddresses);
mail.setSubject('Test Subject'); 
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); // sendEmail - this method sends email
}
}
}

Please make sure you bulkify the code before you send it to production.

Thanks
This was selected as the best answer
Amit Chaudhary 8Amit Chaudhary 8
Update your code like below
public class sendemail_contacts 
{
	public static void sendEmail(){  // create a method
		list<contact> lstcont=new list<contact>();    
		for(contact cont : [select id,name from contact where lastname!=''] )
		{
			Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();  
			string body = 'Hi ';
			String[] toAddresses = new String[] {'example@gmail.com'}; 
			mail.setToAddresses(toAddresses);
			mail.setSubject('Test Subject'); 
			Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); // sendEmail - this method sends email
		}
	}
}

Let us know if this will help you
 
M AnM An
Thanks, NForce and Amit for your reply. Its small thing but I cant overcome that..you have replied me in an understandable way. Your both answers are same and helpful. But I am selecting NForce answer as the best answer.

Your explanation really helpful Nforce.