1

I have been trying to do the Batch job trailhead module, but am receiving 2 errors for my LeadProcessorTest class - please help?

Errors

Line 9 - Field is not writeable: Lead.Name

Line 22 - Variable does not exist: lds

Apex Class

global class LeadProcessor implements Database.Batchable<sObject> {

global Database.QueryLocator start(Database.BatchableContext bc) {
    return Database.getQueryLocator(
        // Get all Leads
                [SELECT Id, Name, LeadSource FROM Lead]);
}

global void execute(Database.BatchableContext bc, List <Lead> scope) {
    List<Lead> updateLeads = new List<Lead>();
    for(Lead ld : scope) {
        updateLeads.add(new Lead(
        // assign leadsource the value dreamforce
        id = ld.id,
        LeadSource = 'Dreamforce'));
        updateLeads.add(ld);
    }
    //update all leads in scope - in querylocator
    update scope;
}
// exit the batch - doing nothing. 
global void finish(Database.BatchableCOntext bc) {}
}

Test Class

@isTest
private class LeadProcessorTest {

@testSetup static void setup() {
    // create new list for leads
    List<Lead> lds = new List<Lead>();
    // insert 200 records
    for(Integer i=0; i<200; i++) {
        lds.add(new Lead(
            name = 'Lead ' + i,
            LeadSource = null));
    }
    insert lds;
}
static testmethod void test() {
    Test.startTest();
    LeadProcessor lp = new LeadProcessor();
    Id batchId = Database.executeBatch(lp);
    Test.stopTest();

    // after all the testing stops, assert leadsource = Dreamforce 
    System.assertEquals('Dreamforce', lds.LeadSource);
}
}
  • Also, trailhead says this "Challenge Not yet complete... here's what's wrong: The 'LeadProcessorTest' test class doesn't appear to be using the 'LeadProcessor' class." – CharniW Jan 18 '18 at 11:31
1

So, a few things here

  1. Name is a compound field. To set the Name on a new Lead you actually need to pass FirstName and LastName instead
  2. In your test method, the variable lds indeed does not exist. You will need to load the leads with something like

    List<Lead> lds = [SELECT LeadSource FROM Lead];
    
  3. Once you've done that, you'll need to loop through each lead in that list individually asserting it's LeadSource is correctly set
  4. In your execute function you populate a list of Leads called updateLeads but you do nothing with it - you can delete that code ;)
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.