How to set created date for a test method in test class in salesforce?
System.Test allows you to set the created date/time of a record.
This is super helpful when inserting test data and you want some of it to appear to the older.
// Sets CreatedDate for a test-context sObject.
syntax : setCreatedDate(recordId,createdDatetime)
ex :
@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.
No comments:
Post a Comment