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
RAJU_DEEPRAJU_DEEP 

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

 

Anand@SAASAnand@SAAS

You will have to iterate over your dml2__c object and populate the toAddresses. Something like below:

 

toAddresses = new List<String>();
for(dml2__c dmlRec:[select Email__c from dml2__c]){
     toAddresses.add(Email__c);
}
Messaging.MassEmailMessage mail1 = new Messaging.MassEmailMessage();
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 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.

 

RAJU_DEEPRAJU_DEEP

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.

NaishadhNaishadh

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

RAJU_DEEPRAJU_DEEP

Hello,

           Thanks for your response, I tried for mass email message but it shows the exception. Here are both:

 

 

trigger MassEmailAttendance on Attendance_Detail__c (after insert) {
final String template = 'test';

Messaging.MassEmailMessage message = new Messaging.MassEmailMessage();

message.setTemplateId([select Id from EmailTemplate where Name = :template].Id);

// Send mass email to contacts of account associated with each updated case
for (Attendance_Detail__c ad : Trigger.new) {
//Contact[] contacts = [select HasOptedOutofEmail, Id from Contact where AccountId = :uc.AccountId];
Attendance_Detail__c[] att = [select User_Email_Id__c from Attendance_Detail__c where User_Email_Id__c =: ad.User_Email_Id__c];
Id[] targetObjectIds = new Id[] {};
Id[] whatIds = new Id[] {};

for (Attendance_Detail__c c : att) {
targetObjectIds.add(c.Id);
whatIds.add(ad.Id);
}

message.setTargetObjectIds(targetObjectIds);
message.setWhatIds(whatIds);

Messaging.sendEmail(new Messaging.Email[] {message});
}
}

 This is the exception:

 

 

 

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, demoPackageTest.MassEmailAttendance: execution of AfterInsert caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_ID_FIELD, Only contacts, leads and users allowed for targetObjectIds.: [] Trigger.demoPackageTest.MassEmailAttendance: line 23, column 5: []

 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.