You need to sign in to do that
Don't have an account?
p 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){
}
}
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){
}
}
Can you try the below test class. It gives 100% coverage
Let me know if you face any issues.
If this solution helps, Please mark it as best answer.
Thanks,
All Answers
Can you try the below test class. It gives 100% coverage
Let me know if you face any issues.
If this solution helps, Please mark it as best answer.
Thanks,