You need to sign in to do that
Don't have an account?
unable to send mass email through triggers.
Hello,
I am in scenerio in which whenever a new record is inserted by the user with the last name 'kumar' an email should be send to the email after fetching the email id from the email field present in the new record. This thing i am trying through trigger and that code is:
trigger updateDml2 on dml2__c (after insert, after Update) {
String[] toAddresses;
string message;
for(dml2__c d2 : Trigger.new){
if(d2.Last_Name__c == 'kumar'){
String emailID = [select Email__c from dml2__c where Last_Name__c = 'kumar'].Email__c;
Messaging.SingleEmailMessage mail1 = new Messaging.SingleEmailMessage();
toAddresses = new String[] {emailID};
mail1.setToAddresses(toAddresses);
message = 'Email alert when the new record is populated with the last name kumar...!!!';
mail1.setHtmlBody(message);
Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail1});
}
}
}
But the problem i am facing is:
This code is running fine when the record is inserted by one user as it return by query only one email id, but it generates the error when the query return the multiple records.
For the mass email i tried this code but it show the following error. I think the query returning the records of Email__c should be placed into toAddresses but i unable to do this.
Error: Compile Error: Initial term of field expression must be a concrete SObject: LIST<demoPackageTest__dml2__c> at line 16 column 41 |
List<dml2__c> emailID = [select Email__c from dml2__c]; Messaging.MassEmailMessage mail1 = new Messaging.MassEmailMessage(); toAddresses = new String[] {emailID.Email__c}; mail1.setToAddresses(toAddresses); message = 'Email alert when the new record is populated with the last name kumar...!!!'; mail1.setHtmlBody(message); Messaging.sendEmail(new Messaging.MassEmailMessage[]{mail1});
I am unable to figure out the problem. Is there any way to solve this problem.
Thanks in advance.
Raju
You will have to iterate over your dml2__c object and populate the toAddresses. Something like below:
I also noticed that you are calling "Messaging.sendEmail()" inside the for loop. You will hit governor limits with that approach. I would populate a List<Messaging.MassEmailMessage> and then make the call to Messaging.SendEmail() once.
hello,
Thanks for your reply, I tried your code but it shows the following error:
Error: Compile Error: Method does not exist or incorrect signature: [Messaging.MassEmailMessage].setToAddresses(LIST<String>) at line 26 column 13
Thanks.
You cann't use setToAddresses for mass email. Use setTargetObjectIds and setWhatIds
Ref:http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_email_outbound_mass.htm
Hello,
Thanks for your response, I tried for mass email message but it shows the exception. Here are both:
This is the exception:
So, i unable to send mass email for the custom object. So is there any way to do this. Any idea or code regarding this will be helpful for me.
Thanks
Raju.