Apex テストクラスで作成日SetCreatedDateをテストする方法

環境
Salesforce

書式
setCreatedDate(recordId, createdDatetime)
テストコンテキスト sObject の CreatedDate を設定します。
recordId
型: Id sObject の ID。

createdDatetime
型: Datetime
sObject の CreatedDate 項目に割り当てる値。
データベースの変更はテスト終了時にすべてロールバックされます。

サンプルコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
@isTest
private class SetCreatedDateTest {
static testMethod void testSetCreatedDate() {
Account acc = new Account(name='myAccount');
insert acc;
Test.setCreatedDate(acc.Id, DateTime.newInstance(2020,11,11));
Test.startTest();
Account myAccount = [SELECT Id, Name, CreatedDate FROM Account
WHERE Name ='myAccount' limit 1];
System.assertEquals(myAccount.CreatedDate, DateTime.newInstance(2012,12,12));
Test.stopTest();
}
}
@isTest private class SetCreatedDateTest { static testMethod void testSetCreatedDate() { Account acc = new Account(name='myAccount'); insert acc; Test.setCreatedDate(acc.Id, DateTime.newInstance(2020,11,11)); Test.startTest(); Account myAccount = [SELECT Id, Name, CreatedDate FROM Account WHERE Name ='myAccount' limit 1]; System.assertEquals(myAccount.CreatedDate, DateTime.newInstance(2012,12,12)); Test.stopTest(); } }
@isTest 
private class SetCreatedDateTest {
    static testMethod void testSetCreatedDate() {
        Account acc = new Account(name='myAccount');
        insert acc;
        Test.setCreatedDate(acc.Id, DateTime.newInstance(2020,11,11));
            
        Test.startTest();
        Account myAccount = [SELECT Id, Name, CreatedDate FROM Account 
                             WHERE Name ='myAccount' limit 1];
        System.assertEquals(myAccount.CreatedDate, DateTime.newInstance(2012,12,12));
        
            Test.stopTest();
    }
}

 

Apex

Posted by arkgame