You need to sign in to do that
Don't have an account?
SAHG-SFDC
Trying to complete a challenge on trail head -Create an Apex class that implements the Database.Batchable interface to update all Lead records in the org with a specific LeadSource. Write unit tests that achieve 100% code coverage for the class
global class LeadProcessor implements
Database.Batchable<Leads>, Database.Stateful {
// Database.Batchable<Sobject>, Database.Stateful {
// instance member to retain state across transactions
global Integer recordsProcessed = 0;
global Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator([Select LastName From Leads ]);
}
}
global void execute(Database.BatchableContext bc, List<Leads> scope){
// process each batch of records
List<Leads> leads = new List<Leads>();
for (Leads:leads) {
Leads.LeadSource = Dreamforce;
// add Leads to the list to be updated
leads.add(leads);
// increment the instance member counter
recordsProcessed = recordsProcessed + 1;
}
}
update leads;
}
global void finish(Database.BatchableContext bc){
System.debug(recordsProcessed + ' records processed. Shazam!');
AsyncApexJob job = [SELECT Id, Status, NumberOfErrors,
JobItemsProcessed,
TotalJobItems, CreatedBy.Email
FROM AsyncApexJob
WHERE Id = :bc.getJobId()];
// call some utility to send email
EmailUtils.sendMessage(a, recordsProcessed);
}
}
Database.Batchable<Leads>, Database.Stateful {
// Database.Batchable<Sobject>, Database.Stateful {
// instance member to retain state across transactions
global Integer recordsProcessed = 0;
global Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator([Select LastName From Leads ]);
}
}
global void execute(Database.BatchableContext bc, List<Leads> scope){
// process each batch of records
List<Leads> leads = new List<Leads>();
for (Leads:leads) {
Leads.LeadSource = Dreamforce;
// add Leads to the list to be updated
leads.add(leads);
// increment the instance member counter
recordsProcessed = recordsProcessed + 1;
}
}
update leads;
}
global void finish(Database.BatchableContext bc){
System.debug(recordsProcessed + ' records processed. Shazam!');
AsyncApexJob job = [SELECT Id, Status, NumberOfErrors,
JobItemsProcessed,
TotalJobItems, CreatedBy.Email
FROM AsyncApexJob
WHERE Id = :bc.getJobId()];
// call some utility to send email
EmailUtils.sendMessage(a, recordsProcessed);
}
}
Thanks
Amit Chaudhary
All Answers
Thanks
Amit Chaudhary
Yes, It does help, Thanks a lot
I have updated and it works
Thanks!
@isTest
public class LeadProcessorTest
{
static testMethod void testMethod1()
{
List<Lead> lstLead = new List<Lead>();
for(Integer i=0 ;i <200;i++)
{
Lead led = new Lead();
led.FirstName = 'FirstName';
led.LastName = 'LastName' +i;
led.Company = 'demo' +i;
lstLead.add(led);
}
insert lstLead;
Test.startTest();
LeadProcessor obj = new LeadProcessor();
Database.executeBatch(obj);
Test.stopTest();
}
}
I am getting below error
Error: Compile Error: Method does not exist or incorrect signature: Test.startTest() at line 16 column 9
Could you please help me out
Thanks
Thanks
The codes were all good but still it wasnot working for me . Later I realized that in the testing class when creating a new Lead record, we need to be careful about the fileds that are mandatory for lead records. In my case, I was missing StartDate and EndDate. Here is the code below :
@isTest
public class LeadProcessorTest
{
static testMethod void testMethod1()
{
List<Lead> listLead = new List<Lead>();
for(Integer i=0 ;i<200;i++)
{
Lead led = new Lead();
led.FirstName ='FirstName';
led.LastName ='LastName'+i;
led.Company ='Company'+i;
led.TTSApp__StartDate__c = date.today();
led.TTSApp__EndDate__c =date.today() + 2;
listLead.add(led);
}
insert listLead;
Test.startTest();
LeadProcessor obj = new LeadProcessor();
DataBase.executeBatch(obj);
Test.stopTest();
}
}
I have 1 class and a class test (my 1st in apex). I had to use Batchable to update all entries in a table.
But I'm not able to test more than 60% of the code, and it refuse to deploy.
here is my codes :
and the test class :
But this indicate that the test is covering only 60% of the main class. I try to add code, asserts... No way to go hover 60%.
Please help
regards
David
Hi
I am not able to cover 100%test coverage even with the above code .
I am trying to do exactly as above for this challenge, no errors , test is successful. However, when I check challenge in my trailhead, it gives me below message & so I am not able to complete this challenge. Can someone please advise?
"The 'LeadProcessor' class did not achieve 100% code coverage via your test methods. Make sure that you chose 'Run All' tests in the Developer Console at least once before attempting to verify this challenge."
LeadProcess Class
public class LeadProcessor implements
Database.Batchable<sObject>
{
public Database.QueryLocator start(Database.BatchableContext bc)
{
return Database.getQueryLocator(
[SELECT LeadSource FROM Lead ]);
}
public void execute(Database.BatchableContext bc, List<Account> scope)
{
// process each batch of records
List<Lead> leads = new List<Lead>();
for (Lead lead : leads)
{
lead.LeadSource = 'Dreamforce';
leads.add(lead);
}
update leads;
}
public void finish(Database.BatchableContext bc)
{
}
}
LeadProcessorTest class
@isTest
private class LeadProcessorTest {
@testSetup
static void setup() {
List<Lead> leads = new List<Lead>();
// insert 200 leads
for (Integer i=0;i<200;i++) {
Lead l = new Lead();
l.LastName = 'Last' + i;
l.Company = 'myCompany' + i;
l.Status = 'Open - Not Contacted';
leads.add(l);
}
insert leads;
}
static testmethod void test() {
Test.startTest();
LeadProcessor mld = new LeadProcessor();
Database.executeBatch(mld);
Test.stopTest();
// after the testing stops, assert records were updated properly
// System.assertEquals(200, [select count() from Lead where LeadSource = 'Dreamforce']);
}
}
There was some typos & additional lines in my execute() method. I was able to rectify it & successfully able to complete the challenge -
Here is the updated one.
public void execute(Database.BatchableContext bc, List<Lead> leads). // -->changed type error from Account to Lead
{
// process each batch of records
// List<Lead> leads = new List<Lead>(); -->commented out
for (Lead lead : leads)
{
lead.LeadSource = 'Dreamforce';
// leads.add(lead); ---> commented out
}
update leads;
"No more than one executeBatch can be called from within a test method"
This occured because I did not set a limit to my query and was trying to fetch more than 200 records from Leads - this will throw the above error while running your test class because a batch can process no more than 200 records per transaction. To avoid this exception please use the below code in your LeadProcessor class:
Hope this helps!
Regards,
Parikshit Sarkar