You need to sign in to do that
Don't have an account?
Arpitha Gowda
How do i write test class for the batch apex
Hello All
How do i write test class for below Batch apex class,
How do i write test class for below Batch apex class,
global class Emailalertbatchclass implements Database.Batchable<sObject>, Schedulable, Database.Stateful { //Variable Section global FINAL String strQuery; global FINAL String leadid; global List<String> errorMessages = new List<String>(); global Emailalertbatchclass() { this.strQuery = getBatchQuery(); } //Returns the Query String to Batch constructor to fetch right records. private String getBatchQuery() { String strQuery = 'SELECT Id,Name,Status,Email,owner.email,owner.name,ownerid,No_Enquiry_Email_Sent__c,Manager_Email__c FROM Lead where No_Enquiry_Email_Sent__c=false AND Status=\'Enquiry\' And (CreatedDate = YESTERDAY OR LastModifiedDate = YESTERDAY) limit 1'; return strQuery; } //Batch Start method global Database.QueryLocator start(Database.BatchableContext BC) { return Database.getQueryLocator(strQuery); } //Batch Execute method calls findCostForWoD method global void execute(Database.BatchableContext BC, List<sObject> scopeList) { System.debug(LoggingLevel.INFO, '== scopeList size ==' + scopeList.size()); List<Lead> ld = (List<Lead>) scopeList; List<Lead> updatedld = new List<Lead>(); if(!ld.isEmpty()) { List<Messaging.SingleEmailMessage> mailList = new List<Messaging.SingleEmailMessage>(); for (Lead prod : ld) { // Step 1: Create a new Email Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); // Step 2: Set list of people who should get the email String[] toAddresses = new String[] {prod.owner.Email,prod.Manager_Email__c,'chandra.s@proseraa.com'}; mail.setToAddresses(toAddresses); // Step 3: Set who the email is sent from mail.setReplyTo(prod.owner.Email); mail.setSenderDisplayName('No Activity on Leads for 24hrs'); // (Optional) Set list of people who should be CC'ed List<String> ccTo = new List<String>(); ccTo.add('manjunath.s@proseraa.com'); mail.setCcAddresses(ccTo); // Step 4. Set email contents - you can use variables! mail.setSubject('No Activity on Lead for 24hrs'); String body = 'Dear ' + prod.owner.name + ', <br><br>'; body += 'This is to notify you that there is no activity done on the respective <b> Lead Name: '; body +=prod.Name+'</b> please find the link below..<br><br>'; body += 'link to file: https://moengage--proseraa.lightning.force.com/lightning/r/Lead/'+prod.id+'/view'+'<br><br><br> Thanks,<br>Moengage Team</body></html>'; mail.setHtmlBody(body); // Step 5. Add your email to the master list mailList.add(mail); prod.No_Enquiry_Email_Sent__c = true; updatedld.add(prod); } if(!mailList.isEmpty()) { try{ Messaging.sendEmail(mailList); update updatedld; } catch (Exception ex) { errorMessages.add('Unable to send email to Tech: '+ ex.getStackTraceString()); } } } } //Batch Finish method for after execution of batch work global void finish(Database.BatchableContext BC) { } //Method which schedules the ProductDownloadBatch global void execute(SchedulableContext sc) { Emailalertbatchclass snInstance = new Emailalertbatchclass(); ID batchprocessid = Database.executeBatch(snInstance); } }
It will work fine then.
See the above Class and try to modify your code accordinly.
All Answers
We have a test method named as setCreatedDate which needs 2 parameters. ID and Dateatime.
Please see the Code bellow.
it is not showing any Code Coverage, any suggestions??
Please check if there is any error while running the test class. There might be a required field missing or something.
i tried modifying youtr code and got 28% coverage
i am getting an error in the test results - System.NoAccessException: Use Test.setCreatedDate() only before Test.startTest().
for Test.setCreatedDate(ld.Id, yesterday);
How do i correct it?
It will work fine then.
See the above Class and try to modify your code accordinly.
below is my Updated Code
i forgot to Define the RecordTypeID
This is my Schedular class
is is my test class