(Note: The feature we are discussing here is as per the Salesforce Spring 16 preview release notes,So this feature is available from Spring 16 release of Salesforce, but features may or may not be available after the Spring 16 go to live )
Before Spring 16 if you are inserting let assume 20 test records in salesforce test class for all those 20 test records the created date is same .If you are working any scenario like to fetch only latest records 20 you will be getting all that 20 records with same created date because all records are inserted with same date so you can't guaranty that in which order they are coming even if you are using order by created date.
From Spring 16 release releasing this great feature to set the created date by the developer for the test records in salesforce test classes (It may or may not be released in Spring as per the note above).
The new Test class method called Test.setCreatedDate(RecordId,DateTime) which allows to set the created date for the test records in salesforce test class.
How to use:
1 . Create a test record in test class.
2. Pass that record Id along with date time that you want set to Test.setCreatedDate(Id,DateTime).
Before Spring 16 if you are inserting let assume 20 test records in salesforce test class for all those 20 test records the created date is same .If you are working any scenario like to fetch only latest records 20 you will be getting all that 20 records with same created date because all records are inserted with same date so you can't guaranty that in which order they are coming even if you are using order by created date.
From Spring 16 release releasing this great feature to set the created date by the developer for the test records in salesforce test classes (It may or may not be released in Spring as per the note above).
The new Test class method called Test.setCreatedDate(RecordId,DateTime) which allows to set the created date for the test records in salesforce test class.
How to use:
1 . Create a test record in test class.
2. Pass that record Id along with date time that you want set to Test.setCreatedDate(Id,DateTime).
@isTest
private class SetCreatedDateTest {
static testMethod void testSetCreatedDate() {
Account a = new Account(name='Test Account');
insert a;
Test.setCreatedDate(a.Id, DateTime.newInstance(2012,12,12));
Test.startTest();
Account myAccount = [SELECT Id, Name, CreatedDate FROM Account
WHERE Name ='Test Account' limit 1];
System.assertEquals(myAccount.CreatedDate, DateTime.newInstance(2012,12,12));
Test.stopTest();
}
}
Key points:
1.All database changes are rolled back at the end of a test. You can’t use this method on records that existed before your test executed.
2.You also can’t use setCreatedDate in methods annotated with @isTest(SeeAllData=true), because those methods have access to all data in your org.
3.This method takes two parameters—an sObject ID and a Datetime value—neither of which can be null.
4. You can see the doc related to this setCreatedDate at Reference Doc for SetCreatedDate.
I think now everyone is eagerly waiting for this new feature.Enjoy....!
No comments:
Post a Comment