I have a batch process that is sending emails to Users. Is there a limit on the number of emails that I can send to users thru a batch process?
Here is my batch code:
global class InventoryWishlistNotifyBatch implements Database.Batchable<sObject> {
public string query = 'select Id, Owner.Email, Year__c, Make__c, Model__c, Color__c, Account__r.Name, Account__c from Inventory_Wishlist__c where Record_Status__c = \'Open\'';
global database.querylocator start(Database.BatchableContext BC)
{
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, Sobject[] scope)
{
Map<Inventory_Wishlist__c, Product2> temp = new Map<Inventory_Wishlist__c, Product2>();
EmailTemplate emailTemplate = [select Id, Body from EmailTemplate where DeveloperName = 'Inventory_Wishlist_Match_Found'];
for (Inventory_Wishlist__c item : (List<Inventory_Wishlist__c>)scope) {
for(Product2 prod : getProducts()) {
if(item.Make__c.trim().toLowerCase() == prod.Make__c.trim().toLowerCase() &&
item.Model__c.trim().toLowerCase() == prod.Model__c.trim().toLowerCase() &&
item.Year__c.trim().toLowerCase() == prod.Year__c.trim().toLowerCase()) {
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setToAddresses(new String[] {item.Owner.Email});
email.setSaveAsActivity(false);
email.setTargetObjectId(item.OwnerId);
email.setTemplateId(emailTemplate.Id);
email.setWhatId(item.Id);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
item.Record_Status__c = 'Closed';
temp.put(item, prod);
}
}
}
if(!temp.isEmpty() && temp.size() > 0) {
List<Inventory_Wishlist__c> itemsToUpdate = new List<Inventory_Wishlist__c>();
itemsToUpdate.addAll(temp.keyset());
update itemsToUpdate;
}
}
global void finish(Database.BatchableContext BC) {
}
private List<Product2> getProducts() {
return [select Id, Make__c, Model__c, Year__c, Color__c from Product2 where Make__c != null AND Model__c != null AND Year__c != null];
}
}
I have a nested loop where I'm scanning all products based on each item in my outer loop which is my scope. If I find a match, I'm sending the email.
Is there a more streamlined approach to scan the products, find a match and send the email?
If the emails are going to internal salesoforce users, does the 1000 emails a day limit apply?