You need to sign in to do that
Don't have an account?
M 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?
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?
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
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
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.
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
Let us know if this will help you
Your explanation really helpful Nforce.