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
Deepika RahangdaleDeepika Rahangdale 

i want test class of batch class to send an email to Opportunity owner. On 1st of each month send an email to Opportunity owner if close date is in this month.

test class of batch class to send an email to Opportunity owner. On 1st of each month send an email to Opportunity owner if close date is in this month.
mukesh guptamukesh gupta
Hi Deepika,

Please follow code,

email code will cover by you main apex class, if you face any difficulties then please share your main class code
 
List<Account> acc = new List<Account>{
		new Account(
					Name= 'Test Account', 
					BillingStreet= 'Street No 12', 
					BillingCity= 'Pune', 
					BillingState= 'MH'
				)
			};
			insert acc;

	List<Contact> conn = new List<Contact>{
		new Contact(LastName= 'Test', 
				FirstName= 'Test1', 
				AccountId= acc[0].Id)
	};
	insert conn;
	Integer currentMonth = Date.Today().Month();
	Opportunity opp = new Opportunity(Name='test opp', 
                                      StageName='Closed/Won', 
                                      Probability = 95, 
                                      CloseDate=system.today(), 
                                      AccountId= acc[0].Id,
                                      Amount = 1000);
	insert opp;

    Date oppCloseddt = opp.CloseDate;
System.debug(currentMonth+'-->> '+oppCloseddt.month());
if(oppCloseddt.month() == currentMonth){
    Test.startTest();
    youBatchClass c = new youBatchClass();
    Database.executeBatch(c);
    Test.stopTest();

}

if you need any assistanse, Please let me know!!


Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh

 
Suraj Tripathi 47Suraj Tripathi 47
Hi,

You can take reference from the below code there are chances that an error can occur as emails have to send on the 1st of every month. In this situation you have to use the @testvisible method. I am attaching a link for it.

- https://salesforce.stackexchange.com/questions/138997/how-to-set-datetoday-time-in-a-test-class
- https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_testvisible.htm


@isTest(seeAllData = true) 

public class TestSendEmailToOppwner  { 

static testMethod void testMethod1(){ 

    Profile p = [SELECT Id FROM Profile WHERE Name='standarduser'];
    User user1 = new User( Alias = 'standt', Email='standarduser@tt.com',  
    EmailEncodingKey='UTF-8', LastName='Testing1', LanguageLocaleKey='en_US',  
    LocaleSidKey='en_US', ProfileId = p.Id,  
    TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@tt.com'); 


    System.runAs(user1) { 
        Account account1 = new Account(); 
            account1.Name = 'Test Account'; 
            insert account1; 
            account1=[SELECT id,Name FROM account WHERE id=:account1.Id]; 
            System.assertEquals(account1.Name,'Test Account'); 
        Opportunity opportunity1 = new Opportunity(); 
            opportunity1.AccountId = account1.Id;  
            opportunity1.Name = 'Testing'; 
            opportunity1.StageName = 'Prospecting'; 
            opportunity1.CloseDate = System.Today();                
            insert opportunity1 ; 
            
            opportunity1=[SELECT id,Name,StageName,CloseDate,Owner.name,Owner.Email FROM Opportunity WHERE id=:opp.Id]; 
          
         }
         Test.StartTest(); 
            Database.executeBatch(new your_class_Name());
        Test.StopTest(); 

}

Please mark it as Best Answer if it helps you.

Thanks & Regards
Suraj Tripathi


 
Deepika RahangdaleDeepika Rahangdale
Hii
mukesh gupta

i got error 
this is my batch class code 
this is execute method 
thanks for help
List<String> emailList=new List<String>();
    public void execute (Database.BatchableContext ct,List<Opportunity> opplist){
        for(Opportunity opp:opplist)
        {
            emailList.add(opp.Owner.Email);
        }
        sendmail();
    }
    public void sendmail()
    { 
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        email.setToAddresses(emailList);
        email.setSubject(' opportunity Close Date!!!!!');
        email.setPlainTextBody('Close date of this opportunity is in this month');
        
        Messaging.sendEmail(New Messaging.SingleEmailMessage[]{email});
    }