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
RahulRahul 

I have a requirement that when there is no opportunity necord uploaded for a today(). then Email should be sent to the system administrator that you have not uploaded the opportunity for today, Reminding him to upload an opportunity

Best Answer chosen by Rahul
v varaprasadv varaprasad
Hi Sumit,

Please check following sample code, I have not tested may you will get some syntactical errors.
 
global class scheduledBatchable implements Schedulable {
    
    global void execute(SchedulableContext sc) {
        
		Messaging.SingleEmailMessage[] messages = new List<Messaging.SingleEmailMessage> {message};
        list<opportunity> opp = [SELECT id, name FROM opportunity WHERE createddate = TODAY];
		if(opp.size() == 0){					
		  Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
			// Set recipients 			
			message.toAddresses = new String[] { 'Admin@gmail.com' };
			message.optOutPolicy = 'FILTER';
			message.subject = 'Opportunity Not created';
			message.plainTextBody = 'Opportunity Not created.Please create';
			messages.add(message);
		    Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);
			if (results[0].success) {
				System.debug('The email was sent successfully.');
			} else {
				System.debug('The email failed to send: '
					  + results[0].errors[0].message);
			}
		
        }
    } 
    
   }
You need to above schedule class daily.

User-added image

Hope this helps you!

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
 

All Answers

v varaprasadv varaprasad
Hi Sumit,

Please check following sample code, I have not tested may you will get some syntactical errors.
 
global class scheduledBatchable implements Schedulable {
    
    global void execute(SchedulableContext sc) {
        
		Messaging.SingleEmailMessage[] messages = new List<Messaging.SingleEmailMessage> {message};
        list<opportunity> opp = [SELECT id, name FROM opportunity WHERE createddate = TODAY];
		if(opp.size() == 0){					
		  Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
			// Set recipients 			
			message.toAddresses = new String[] { 'Admin@gmail.com' };
			message.optOutPolicy = 'FILTER';
			message.subject = 'Opportunity Not created';
			message.plainTextBody = 'Opportunity Not created.Please create';
			messages.add(message);
		    Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);
			if (results[0].success) {
				System.debug('The email was sent successfully.');
			} else {
				System.debug('The email failed to send: '
					  + results[0].errors[0].message);
			}
		
        }
    } 
    
   }
You need to above schedule class daily.

User-added image

Hope this helps you!

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
 
This was selected as the best answer
v varaprasadv varaprasad
Messaging.SingleEmailMessage[] messages = new List<Messaging.SingleEmailMessage>{message};

change to 

Messaging.SingleEmailMessage[] messages = new List<Messaging.SingleEmailMessage>();
RahulRahul
Hi V Varaprasad, Thank you so much. I really appretiate your help. If you dont mind can you Please help me with the test class?
v varaprasadv varaprasad
@isTest
private class scheduledBatchable_Test{
    static testMethod void test_execute_UseCase1(){
        List<Opportunity> opportunity_Obj  =  [SELECT Id,Name,StageName,CreatedDate from Opportunity];
        System.assertEquals(true,opportunity_Obj.size()==0);
        scheduledBatchable obj01 = new scheduledBatchable();   
       
          String CRON_EXP = '0 0 0 3 9 ? 2022';
        system.schedule('Test Territory Check', CRON_EXP, obj01);

        
    }
}

 
RahulRahul
Thank you so much V Vaaraprasad for your help.