0

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?

1

There’s no limit on sending individual emails to contacts, leads, person accounts, and users in your organization directly from account, contact, lead, opportunity, case, campaign, or custom object pages.

see http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_gov_limits.htm

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.