You need to sign in to do that
Don't have an account?
Michael M
Schedule email containing a list of all records owned by each user
I need to send out a monthly email, to every record owner (for 1 of our custom objects), which contains a list of all of their records, with 3 columns (each different fields on each of those records.) Can you please post an example of code that would work for the correct queries?
Here is a sample code. Please make changes as per your requirement
You can use system.scheduleBatch to schedule the batch
If you find this information helpful, please mark this answer as Best. It may help others in the community. Thank You!
Anudeep
All Answers
Here is a sample code. Please make changes as per your requirement
You can use system.scheduleBatch to schedule the batch
If you find this information helpful, please mark this answer as Best. It may help others in the community. Thank You!
Anudeep
global class LeadOwnershipEmail implements Schedulable{
global void execute(SchedulableContext SC) {
list<community_lead__c> recs = [SELECT Referral_Source__c, Services_Needed__c, owner.email, Lead_initiated_by__c, OwnerId FROM Community_Lead__c];
List<Messaging.SingleEmailMessage> mailList = new List<Messaging.SingleEmailMessage>();
Date D =Date.today();
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
List<String> toAddresses = new List<String>();
String messageBody;
list<string> accstring =new list<string>();
for(Community_Lead__c m : recs) {
messageBody = m.Referral_Source__c+'--'+ m.Services_Needed__c+'--'+m.Lead_initiated_by__c ;
accstring.add(messageBody );
toAddresses.add(m.owner.email);
}
mail.setToAddresses(toAddresses);
mail.setSubject('Account Owners');
string allstring = string.join(accstring,'<br/>');
mail.setHtmlBody('<html><body>Hi <br>'+ '<br> Here are the records you own. Kindly contact your administrator if there are any descrepencies.<br>'+allstring +'<br><b>Regards,</b><br>Regards <br/></body></html>
mailList.add(mail);
Messaging.sendEmail(mailList);
}
}
Here is what I recommend to ensure it only sends each owner the records that he owns
Query for an additional field - CreatedById in the SOQL query and compare it with OwnerID and only send the email if it matches. Something like this
Doing this with Scheduled Apex instead of the batch is fairly simple. See the syntax in the following documentation
Just move the same code to the execute method. The logic remains the same. Hope this helps
Anudeep