function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

CreatedDate is only precise to one second... how to test something...


I have a Class which does a query, with "order by CreatedDate Desc Null Last Limit 1".  E.g. giving me the most recently created record.


When writing test code I first create a bunch of records, run the class and check the results.  It fails because since the test records are all created within a second of each other in the testing code, the sort order is sometimes incorrect and I'm not always getting the most recently created record.


I want to do this in my testing code:


List<A__c> theAs = new List<A__c>{new A__c(Name='first'), new A__c(Name='second'), new A__c(Name='third, etc.')};

insert theAs;



But, the only way I can see to make this work is to force the CreatedDates of the test data to be more than one second apart with a construct like: (I'm simplifying, leaving out all data, etc.)


A__c a1 = new A__c(Name='or even this one...');

insert a1;


A__c a2 = new A__c(Name='but I sometimes get this one');

insert a2;


A__c a3 = new A__c(Name='I want this one');

insert a3



where killsometime() just burns cpu:


public String killsometime() { String s;for (Integer i=0; i<3000;i++) s = s + i; return s);


So, this solution works, but it's butt-ugly and I'm wondering if there are any better solutions out there?


Thanks, best, Steve.




Bump... anybody?