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
p sonwanep sonwane 

please provide me test class for folloing code as i am new to test classes

global class SendEmailToAccountOwner implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext bc){
        String query = 'SELECT Id,Name,Account__r.id,Account__r.name,Account__r.Owner.email FROM Training__c where CreatedDate = Today';
       System.debug('** query'+query);
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext bc , List<Training__c> batch){
        List<Contact> conlist= new List<Contact>();
        
        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
        List<String> sendTo = new List<String>();
        
        for (Training__c tr : batch) {
            String s ='';
            sendTo.add(tr.Account__r.Owner.email);
            System.debug('send email' +tr.Account__r.Owner.email);
            
            Contact con = new Contact();
            con.lastname= tr.name;
            con.AccountId= tr.Account__c;
            conlist.add(con);
            System.debug('conlist ** '+conlist);
            
            s+=Con.lastname +' has been created for'+ tr.Account__r.name;
            System.debug('s**'+s);
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setSenderDisplayName('Email Alert');
            mail.setSubject('Contact Created  for Account Reated to Training');
            String body = 'Account Related Contacts==> '+s.removeEnd(',');
            mail.setToAddresses(sendTo);
            mail.setHtmlBody(body);
            mails.add(mail);
        }
        insert conlist;
        Messaging.SendEmail(mails);  
    }
    global void finish(Database.BatchableContext bc){
        
    }
}
Best Answer chosen by p sonwane
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you try the below test class. It gives 100% coverage
 
@istest
public class SendEmailtoAccountOwnerTest {

    static testMethod void sendemail(){
        Account acc = new Account();
        acc.name='sample';
        insert acc;
        Training__c tr = new Training__c();
        tr.account__c= acc.id;
        tr.name='sample training';
        insert tr;
        test.startTest();
        SendEmailtoAccountOwner sr= new SendEmailtoAccountOwner();
        database.executeBatch(sr);

    }
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you try the below test class. It gives 100% coverage
 
@istest
public class SendEmailtoAccountOwnerTest {

    static testMethod void sendemail(){
        Account acc = new Account();
        acc.name='sample';
        insert acc;
        Training__c tr = new Training__c();
        tr.account__c= acc.id;
        tr.name='sample training';
        insert tr;
        test.startTest();
        SendEmailtoAccountOwner sr= new SendEmailtoAccountOwner();
        database.executeBatch(sr);

    }
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
This was selected as the best answer
p sonwanep sonwane
It's working thank you