1

I'm getting this error:

Method does not exist or incorrect signature: void setCreatedDate(List<Object__c>, Datetime)

With this (excerpt of) test class below

List<Object__c> testRecords = new List<Object__c>();

for (Integer i = 0; i < 5000; i++) {
    testRecords.add(new Object__c(Name = 'Test Record ' + i));
    DateTime TestCreatedDate = DateTime.now().addDays(-15);
    Test.setCreatedDate(testRecords, TestCreatedDate);
}

insert testRecords;

No problem doing it with a single record Id, but no idea how to do it with a list. This is to test a class that deletes records more than 14 days old (which is working). The test class saves if I remove the Test.setCreatedDate() method, but then the test doesn't pass.

I've omitted the entire class for the sake of not having to redact a bunch of org specific information, but if it's a necessity then I'll begrudgingly amend it for public consumption...

1 Answer 1

2

The documentation says there's only setCreatedDate(Id recordId, Datetime createdDatetime), so you've gotta do this one record at a time

  • Create your test records
  • Insert them
  • Iterate over your test records and call Test.setCreatedDate()
List<Object__c> testRecords = new List<Object__c>();

// Since this is a constant, it should be declared/initialized outside of all loops
DateTime testCreatedDate = DateTime.now().addDays(-15);

// Create records
for (Integer i = 0; i < 5000; i++) {
    testRecords.add(new Object__c(Name = 'Test Record ' + i));
}

insert testRecords;

// Set the date
for(Object__c testObj :testRecords){
    Test.setCreatedDate(testObj.Id, testCreatedDate);
}
2
  • Derek, you're a gentleman and a scholar. That worked! If there's a way of marking a correct answer, I can't find it...
    – Gaffers
    Commented Jan 27, 2023 at 13:45
  • @Gaffers There's a checkmark under the vote section (the up and down arrows). I don't remember if there's a minimum time that you need to wait before accepting an answer though (I answer more questions than I ask).
    – Derek F
    Commented Jan 27, 2023 at 13:50

You must log in to answer this question.

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