• Ria Sharma
  • NEWBIE
  • 5 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 3
    Replies
So I wrote one batch class and it is working fine in sandbox and sending emails to contacts but it is not sending emails in production. It sent couple of emails 2 days back but after that when I tried to excute this, it is giving me error :
15:53:37:722 FATAL_ERROR System.EmailException: SendEmail failed. First exception on row 297; first error: SINGLE_EMAIL_LIMIT_EXCEEDED, Email limit exceeded: []

How come it is working in sandbox? I am so confused as cannot find any way to fix it. I tried to download email logs and it saya 5000 email were sent today so if limit is 1000 emails per day how come 5000 emails went out? Also in sanbox, it is 1500 emails showing up in email logs so this means I am not hitting 1000 emails per day limit?

Can someone help?
I tried writing a batch class but I am having hard time adding one condition. Can someone look and help me out. My requirement is below:
1. Run batch apex on daily basis to send email notification 
2. Email should be send 30 days, 60 days and 90 days before contract end date on opportunity where stage is 8 
3. Email should go to only those contacts in associated account where 'sendemail' checkbox is true. 

Now my problem is how to add third condition and how to send email to contact email address. 
 
global class RenewalEmailNotification implements Database.Batchable < sObject >, Schedulable, Database.Stateful { global List<String> errorMessages = new List<String>(); global Database.QueryLocator start(Database.BatchableContext bc) { /* Date d = Date.today();*/ Date ed = Date.today().addDays(30); System.debug(Date.today().addDays(30)); // This set will hold the Id's of the opportunities Set <ID> OppIds = new Set <ID>(); // Query all the Opportunities in your database that meet the selection criterial for update. //List <Opportunity> oppsToUpdate = new List <Opportunity>([SELECT Id FROM Opportunity]); List <Opportunity> oppsToUpdate = new List <Opportunity>([SELECT Id, StageName FROM Opportunity WHERE StageName IN ('8 Renewal', '8 Renewal (pending validation)', '8 Renewal (validated)', '8 Renewal (Quote Delivered to Customer)') AND Contract_End_Date__c=: ed]); // Return this list of opportunities, which will get passed to our execute() method below. return Database.getQueryLocator('Select id, Name , Owner.Email, Contract_End_Date__c FROM opportunity WHERE Id IN: oppsToUpdate '); } global void execute(Database.BatchableContext bc, List < opportunity > recs) { List < Messaging.SingleEmailMessage > mailList = new List < Messaging.SingleEmailMessage > (); for (opportunity c: recs) { if (c.Account != null) { List < String > toAddresses = new List < String > (); Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); toAddresses.add(c.Account.name); toAddresses.add(c.Owner.Email); mail.setToAddresses(toAddresses); mail.setSubject('Notification Before 30 Days of Contract End Date'); String messageBody = '<html><body>Hi ' + c.Name + ',<br>Your Contract Expires within 30 Days . <br>Kindly take approriate action.<br><br><b>Regards,</b><br>RV</body></html>'; mail.setHtmlBody(messageBody); mailList.add(mail); } } Messaging.sendEmail(mailList); } global void finish(Database.BatchableContext bc) { AsyncApexJob aaj = [Select Id, Status, NumberOfErrors, JobItemsProcessed, MethodName, TotalJobItems, CreatedBy.Email from AsyncApexJob where Id =:BC.getJobId()]; // Send an email to the Apex job's submitter notifying of job completion. Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); String[] toAddresses = new String[] {aaj.CreatedBy.Email}; mail.setToAddresses(toAddresses); mail.setSubject('JOB Salesforce RenewalEmailNotification Finished: ' + aaj.Status); String bodyText='Total Job Items ' + aaj.TotalJobItems + ' Number of records processed ' + aaj.JobItemsProcessed + ' with '+ aaj.NumberOfErrors + ' failures.\n'; bodyText += 'Number of Error Messages ' + errorMessages.size() + '\n'; bodyText += 'Error Message' + String.join(errorMessages, '\n'); mail.setPlainTextBody(bodyText); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); } global void execute(SchedulableContext SC) { RenewalEmailNotification batchable = new RenewalEmailNotification(); database.executebatch(batchable); } }

 
So I wrote one batch class and it is working fine in sandbox and sending emails to contacts but it is not sending emails in production. It sent couple of emails 2 days back but after that when I tried to excute this, it is giving me error :
15:53:37:722 FATAL_ERROR System.EmailException: SendEmail failed. First exception on row 297; first error: SINGLE_EMAIL_LIMIT_EXCEEDED, Email limit exceeded: []

How come it is working in sandbox? I am so confused as cannot find any way to fix it. I tried to download email logs and it saya 5000 email were sent today so if limit is 1000 emails per day how come 5000 emails went out? Also in sanbox, it is 1500 emails showing up in email logs so this means I am not hitting 1000 emails per day limit?

Can someone help?
I tried writing a batch class but I am having hard time adding one condition. Can someone look and help me out. My requirement is below:
1. Run batch apex on daily basis to send email notification 
2. Email should be send 30 days, 60 days and 90 days before contract end date on opportunity where stage is 8 
3. Email should go to only those contacts in associated account where 'sendemail' checkbox is true. 

Now my problem is how to add third condition and how to send email to contact email address. 
 
global class RenewalEmailNotification implements Database.Batchable < sObject >, Schedulable, Database.Stateful { global List<String> errorMessages = new List<String>(); global Database.QueryLocator start(Database.BatchableContext bc) { /* Date d = Date.today();*/ Date ed = Date.today().addDays(30); System.debug(Date.today().addDays(30)); // This set will hold the Id's of the opportunities Set <ID> OppIds = new Set <ID>(); // Query all the Opportunities in your database that meet the selection criterial for update. //List <Opportunity> oppsToUpdate = new List <Opportunity>([SELECT Id FROM Opportunity]); List <Opportunity> oppsToUpdate = new List <Opportunity>([SELECT Id, StageName FROM Opportunity WHERE StageName IN ('8 Renewal', '8 Renewal (pending validation)', '8 Renewal (validated)', '8 Renewal (Quote Delivered to Customer)') AND Contract_End_Date__c=: ed]); // Return this list of opportunities, which will get passed to our execute() method below. return Database.getQueryLocator('Select id, Name , Owner.Email, Contract_End_Date__c FROM opportunity WHERE Id IN: oppsToUpdate '); } global void execute(Database.BatchableContext bc, List < opportunity > recs) { List < Messaging.SingleEmailMessage > mailList = new List < Messaging.SingleEmailMessage > (); for (opportunity c: recs) { if (c.Account != null) { List < String > toAddresses = new List < String > (); Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); toAddresses.add(c.Account.name); toAddresses.add(c.Owner.Email); mail.setToAddresses(toAddresses); mail.setSubject('Notification Before 30 Days of Contract End Date'); String messageBody = '<html><body>Hi ' + c.Name + ',<br>Your Contract Expires within 30 Days . <br>Kindly take approriate action.<br><br><b>Regards,</b><br>RV</body></html>'; mail.setHtmlBody(messageBody); mailList.add(mail); } } Messaging.sendEmail(mailList); } global void finish(Database.BatchableContext bc) { AsyncApexJob aaj = [Select Id, Status, NumberOfErrors, JobItemsProcessed, MethodName, TotalJobItems, CreatedBy.Email from AsyncApexJob where Id =:BC.getJobId()]; // Send an email to the Apex job's submitter notifying of job completion. Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); String[] toAddresses = new String[] {aaj.CreatedBy.Email}; mail.setToAddresses(toAddresses); mail.setSubject('JOB Salesforce RenewalEmailNotification Finished: ' + aaj.Status); String bodyText='Total Job Items ' + aaj.TotalJobItems + ' Number of records processed ' + aaj.JobItemsProcessed + ' with '+ aaj.NumberOfErrors + ' failures.\n'; bodyText += 'Number of Error Messages ' + errorMessages.size() + '\n'; bodyText += 'Error Message' + String.join(errorMessages, '\n'); mail.setPlainTextBody(bodyText); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); } global void execute(SchedulableContext SC) { RenewalEmailNotification batchable = new RenewalEmailNotification(); database.executebatch(batchable); } }

 
Hello All,
I had a requirement where i need to send an email notification 100 days before the contract end date.So based on this we had a batch and schedule class written.Now we would like to add few more condition in the code.
We have two picklist value as 'Status Renewed' and "Status Renewed next Year".This picklist field has some values  as Status renewed for nxt quarter , pipeline etc the other picklist has renewed and renewed lost.
Condition is when the "Status Renewed" is equal to "Status renewed for next quarter " or "Pipeline" AND when "Status Renewed next Year" is not equal to "Renewed" or"Renewed lost".
So how do i give this condition in an execute method.When this condition is satisfied ,then the email notification should be sent to the owner before 100 days.

Batch and Schedule Class:
global class NotificationEmailtoAccountExecutive implements Database.Batchable < sObject >, Schedulable, Database.Stateful {
	global List<String> errorMessages = new List<String>();
    global Database.QueryLocator start(Database.BatchableContext bc) {
     
        Date ed = Date.today().addDays(100);
        System.debug(Date.today().addDays(100));
        
        set<Id> setContractIds = new set<Id>();

        for(Contract_role__c objContract: [SELECT  Contract__c FROM Contract_role__c WHERE Role__c = 'Subscription Administrator' AND Contract__r.EndDate =: ed]) {
			setContractIds.add(objContract.Contract__c);
        }
        
        return Database.getQueryLocator('Select  id, Contract_Name__c , EndDate ,Contact_Email__c, Contract_End_Date_2__c, Owner.Email, Owner.Manager.Email ,Account.Owner.Email,Account.Owner.Manager.Email  FROM Contract  WHERE Id IN: setContractIds');
    }

    global void execute(Database.BatchableContext bc, List < Contract > recs) {
        List < Messaging.SingleEmailMessage > mailList = new List < Messaging.SingleEmailMessage > ();
        for (Contract c: recs) {
            if (c.Contact_Email__c != null) {
                List < String > toAddresses = new List < String > ();
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                toAddresses.add(c.Contact_Email__c);
	        toAddresses.add(c.Owner.Email);
                toAddresses.add(c.Account.Owner.Email);
                toAddresses.add(c.Owner.Manager.Email);
                toAddresses.add(c.Account.Owner.Manager.Email);
                mail.setToAddresses(toAddresses);
                mail.setSubject('Notification Before 100 Days of Contract End Date');
                String messageBody = '<html><body>Hi ' + c.Contract_Name__c  + ',<br>Your  Contract Expires within 100 Days . <br>Kindly take  action.<br><br><b>Regards,</b><br>ADP</body></html>';
                mail.setHtmlBody(messageBody);
                mailList.add(mail);
            }
        }
        Messaging.sendEmail(mailList);
    }

    global void finish(Database.BatchableContext bc) {
		AsyncApexJob aaj = [Select Id, Status, NumberOfErrors, JobItemsProcessed, MethodName, TotalJobItems, CreatedBy.Email from AsyncApexJob where Id =:BC.getJobId()];
        
        // Send an email to the Apex job's submitter notifying of job completion.
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {aaj.CreatedBy.Email};
        mail.setToAddresses(toAddresses);
        mail.setSubject('JOB Salesforce NotificationEmailtoAccountExecutive Finished: ' + aaj.Status);
        String bodyText='Total Job Items ' + aaj.TotalJobItems + ' Number of records processed ' + aaj.JobItemsProcessed + ' with '+ aaj.NumberOfErrors + ' failures.\n';
        bodyText += 'Number of Error Messages ' + errorMessages.size() + '\n';
        bodyText += 'Error Message' + String.join(errorMessages, '\n');
        mail.setPlainTextBody(bodyText);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
	}
	
	global void execute(SchedulableContext SC) {
        NotificationEmailtoAccountExecutive batchable = new NotificationEmailtoAccountExecutive();
		database.executebatch(batchable);
    }
}
Any help very much appreciated.