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
RajiRaji 

how to acheive the day email limitation for Organization

i wrote the batch apex code i.e,

global class batchOrphanedContacts implements Database.Batchable<sObject>,schedulable {
	global string strQuery;
	 
    global void execute(schedulablecontext sc){
       batchOrphanedContacts bc= new batchOrphanedContacts();
       database.executebatch(bc);
    }
    
    global Database.QueryLocator start(Database.BatchableContext bc){
    	string Cname='TestFound Contact';
        return Database.getQueryLocator('select Id,lastname,AccountId,owner.email from Contact where name=:Cname  ');   
    }           
 
    global void execute(Database.BatchableContext bc,List<Contact> lstCon){
          string MesgBody;
          string[] Email;
          list<OpportunityContactRole> lstOPPConrole;
          list<Contact> lstUpdateContact=new list<Contact>();
          Contact objCon;
          map<Id,Integer> mapConCount = new map<Id,Integer>();
          map<Id,Contact> mapContacts=new map<Id,Contact>();
          map<Id,Id> mapConAccount = new map<Id,Id>();
                   
          for(Contact objContact:lstCon)
              mapContacts.put(objContact.id,objContact);
              
          lstOPPConRole=[select id,ContactId,OpportunityId,Opportunity.AccountId from OpportunityContactRole where ContactId in: mapContacts.keyset() order by ContactId];
           
           if(lstOPPConRole != null && !lstOPPConRole.isEmpty()){
            for(OpportunityContactRole oppConRole : lstOppConRole){
                if(mapConCount.get(oppConRole.ContactId) != null){
                    if(mapConAccount.get(oppConRole.ContactId) != OppConRole.Opportunity.AccountId)
                        mapConCount.put(oppConRole.ContactId,(mapConCount.get(oppConRole.ContactId)+1));    
                }
                else{
                    mapConCount.put(oppConRole.ContactId,1);
                    mapConAccount.put(oppConRole.ContactId,OppConRole.Opportunity.AccountId);       
                }
            }   
           }
           
            for(Integer iCount=0;iCount<mapContacts.values().size();iCount++){
                objCon = mapContacts.values()[iCount];
                MesgBody = '';
                if(mapConCount.get(objCon.id) != null){
                    if(mapConCount.get(objCon.id) == 1){
                        objCon.accountId = mapConAccount.get(objCon.id);
                        //Found
                         MesgBody = 'Account found for the Contact '+ '\r\r\n' + URL.getSalesforceBaseUrl().toExternalForm() + '/' + objcon.id;
                         Email=new string[]{objcon.owner.email};
                         lstUpdateContact.add(objCon);
                    }else if(mapConCount.get(objCon.id)>1){
                        //Many  
                        MesgBody = 'Too many Accounts found for the Contact'+ '\r\r\n' + URL.getSalesforceBaseUrl().toExternalForm() + '/' + objcon.id;
                        Email=new string[]{objcon.owner.email};
                    }   
                }
                else{
                    //Not Found
                    MesgBody = 'No Accounts found for the Contact'+ '\r\r\n' + URL.getSalesforceBaseUrl().toExternalForm() + '/' + objcon.id;
                    email=new string[]{objcon.owner.email};
                } 
                
                if(MesgBody != ''){
                	if(sendMail(MesgBody,Email) == false){
                		break;		
                	}	
                }
            }
        update lstUpdateContact;
     }
    //send Email 
    public boolean sendMail(string body,string[] toAddresses ) {
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        email.setSubject('Review Orphaned Contact');
        email.setToAddresses( toAddresses );
        email.setPlainTextBody( body );     
        try{
          Messaging.SendEmailResult [] r =Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
          return true;	
        }catch(Exception ex){
          return false;  
        }  
    }
     
    global void finish(Database.BatchableContext bc){      
       system.debug('all done.');   
    }
}

 i need to stop sending  mails after Completing 1000 mails for a day . please help me 

. Advance thanks

 

Regards

VN

Best Answer chosen by Admin (Salesforce Developers) 
forecast_is_cloudyforecast_is_cloudy

Unfortunately, I don't think there is any way to check if you're at or near the 1000 email limit without actually calling 'sendEmail'. You can put the 'sendEmail' call in a try-catch block (as you have done) and then check for the 'SINGLE_EMAIL_LIMIT_EXCEEDED' error code to see if the sendEmail failed because the limit was reached. Hope this helps.

All Answers

forecast_is_cloudyforecast_is_cloudy

Unfortunately, I don't think there is any way to check if you're at or near the 1000 email limit without actually calling 'sendEmail'. You can put the 'sendEmail' call in a try-catch block (as you have done) and then check for the 'SINGLE_EMAIL_LIMIT_EXCEEDED' error code to see if the sendEmail failed because the limit was reached. Hope this helps.

This was selected as the best answer
SalmanSalman
Your email limitation goals don’t slow down for Salesforce , so why should you? Send the amount of emails that you need when you need with ease. 
MassMailer lets you send unlimited emails from within Salesforce, no matter which edition you use. Built on the Salesforce platform, it has all the mass email features you’ll ever need.

For more information on the developer's guide please click here for -Developer's guide

For this, you might need to visit and play with this app -Massmailer App

For more details about massmailer please click here Link