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

BhaviBhavi

Hi Raju, 

 

Your approach seems not too much great as you are firing the SOQL query in the loop , So this may can hit your governer limit.

You should get the outside the loop and then create a set of email id and pass it to the  mail1.setToAddresses

 

i.e.

for(User usr : [Select EMAIL from User where lastName = 'kumar' limit 10])

{

lstEmailIds.add(usr.Email);

}

 

and the pass it to the Emailer

 

mail1.setToAddresses(lstEmailIds);

 

Regards,

Bhavi

RAJU_DEEPRAJU_DEEP

Hello,

            Thanks for your reply. I tried your suggestion but i unable to do, can u please tell me  what is "lstEmailIds" as after putting this code it shows this error. So, will you help me....

 


Error: Compile Error: Method does not exist or incorrect signature: lstEmailIds.add(String) at line 6 column 9