You need to sign in to do that
Don't have an account?
Deepika 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.
Please follow code,
email code will cover by you main apex class, if you face any difficulties then please share your main class code
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh
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
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});
}