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
Bob FossilBob Fossil 

Creating Dynamic Apex Query to populate String for outbound email message...

Hi,

 

If anyone has experience with dynamic Apex and SendEmailMmessage, i would appreciate some help on a quick question. I am looking to create a Dynamic APEX Query within a SingleEmailMessage and having trouble getting the query to populate the list, and also how to associate the results of the query with the toAddresses string. So far I have the two parts of my class below. Any advice on how to put them together?

 

List<sObject> L = Database.query(' SELECT Member_Email__c FROM Attendee__c WHERE Dining_paid__c = false 

LIMIT 100'

 

String[] toAddresses = new String[] {'bobfossil@hotmail.com'};  

 

Thanks,

 

Bob

Best Answer chosen by Admin (Salesforce Developers) 
ericszulcericszulc

Might I suggest that you may not need to use dynamic SOQL (which will overcomplicate things if not needed).

 

You could instead do:

 

List<Attendee__c> attendee_list = [SELECT Member_Email__c FROM Attendee__c WHERE Dining_paid__c = false LIMIT 100];

 

List<String> toAddresses = new List<String>();

 

for(Attendee__c a : attendee_list)

{

toAddresses.add(a.Member_Email__c);

}

All Answers

ericszulcericszulc

Might I suggest that you may not need to use dynamic SOQL (which will overcomplicate things if not needed).

 

You could instead do:

 

List<Attendee__c> attendee_list = [SELECT Member_Email__c FROM Attendee__c WHERE Dining_paid__c = false LIMIT 100];

 

List<String> toAddresses = new List<String>();

 

for(Attendee__c a : attendee_list)

{

toAddresses.add(a.Member_Email__c);

}

This was selected as the best answer
Bob FossilBob Fossil

Eric, you are a star. Works a treat and helped me along with understanding the syntax. Thank you

ericszulcericszulc

No problem, glad it worked out.