1

I trying to get better and have built a trigger/test class using my knowledge I have learned however I am running into an error with the test class that for some reason I just cannot resolve and hoping someone can help shed some light on what I am doing wrong or missing and provide guidance please!

I am getting the error: "Variable does not exist: Id"

Test Class Code:

@isTest private class LeadDailyUpdaterTest {
@testSetup 
static void setup() {
    List<Lead> leads = new List<Lead>();
  
    // insert 200 Leads
    for (Integer i=0;i<200;i++) {
        leads.add(new lead(LastName='Lead '+i, FirstName='First '+i, Company='Lead' +i, Status='Closed - No Response'));
    }
    Datetime SetDate = Datetime.now().addDays(-31);
    Test.setCreatedDate(leads.id, SetDate); //This is the line causing the error
    insert leads;
}
static testmethod void test() {
    Test.startTest();
    LeadDailyUpdater uca = new LeadDailyUpdater();
    Id batchId = Database.executeBatch(uca);
    Test.stopTest();
    // after the testing stops, assert records were updated properly
    System.assertEquals(200, [select count() from lead where leadsource = 'Closed - No Response']);
}}

I commented on the line that the dev console is saying the error is coming from.

I have a batch Apex built for running nightly and effectively it pulls records based on last modified date been over 30 days, so test class I am setting up to create 200 leads and set the created date (since that will set the last modified date from what I have seen in KB pages) to 31 days ago so it returns success that all 200 appear.

Any help is greatly appreciated!!

1 Answer 1

6

leads is a List<Lead>, and lists (along with other collections) do not have Ids. The records they contain might, but the list itself will not.

Beyond that, SObject records do not have Ids until they are inserted.

So you need to insert your Leads first, then iterate over the list of leads to call setCreatedDate

static void setup() {
    List<Lead> leads = new List<Lead>();
  
    // insert 200 Leads
    for (Integer i=0;i<200;i++) {
        leads.add(new lead(LastName='Lead '+i, FirstName='First '+i, Company='Lead' +i, Status='Closed - No Response'));
    }

    // insert leads first
    insert leads;

    Datetime setDate = Datetime.now().addDays(-31);
    // then you can call setCreatedDate (needs to be done on each individual record)
    for(Lead l :leads){
        test.setCreatedDate(l.Id, setDate);
    }
}
4
  • Thanks a million!! This is my first running solo trigger, class and test class setup and was so happy got through most solo but this error got me. -- Thanks so much for the assist! Dec 21, 2020 at 21:51
  • When I try to run the test now it no longer has the ID error but I get this now: System.LimitException: Apex CPU time limit exceeded Any idea how to get around this, the line item triggering is line 17 which is the for(Lead 1 :leads){ line of the code. Any insight is appreciated! Dec 21, 2020 at 22:51
  • @CraigPhoenix That's something that should become a separate question, I think. There's no obvious clues as to why you're running into the CPU limit. You'd need to take a look at any triggers you may have on Lead. Using System.debug(Limit.getCPULimit()); before the loop as well as inside the loop could help you narrow down where the issue is. If CPU usage is high before the loop, it's likely an issue with triggers. If it increases a lot between iterations of the loop, then Test.setCreatedDate() may be causing additional DML or just takes a long time to run.
    – Derek F
    Dec 22, 2020 at 14:01
  • I will go ahead and create a new question then - Thanks Derek Dec 23, 2020 at 0:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .