Set Created Date For Test Class sObject in Apex

Set Created Date For Test Class sObject in Apex

Last Updated on June 19, 2023 by Rakesh Gupta

Big Idea or Enduring Question:

  • How can the Created Date be set for an sObject within a test context?

Objectives:

After reading this blog, you’ll be able to:

  • Gain a comprehensive understanding of the setCreatedDate method related to Apex tests
  • Set the created date for the test class record
  • and much more

👉 In the past I’ve written a few articles on Apex. Why not check them out while you are at it?!

  1. Calculate Overall Code coverage in Salesforce
  2. Check if a String is Null, Empty or Blank in Apex

Business Use case

Ben Halpern is a Jr. Salesforce Developer at Gurukul On Cloud (GoC). He created an Apex trigger for the Lead object.

trigger LeadTrigger on Lead (before update) { 
    
    if(Trigger.isBefore){
        if (Trigger.Isupdate){
            for (Lead myLead:trigger.new){
                if(myLead.LeadSource==null && myLead.createddate== Date.today().addDays(-1)){
                    //do something
                }
            }
        }
    }
}

He knows how to write the test class for above code. He requires assistance in creating a record with a past date within the test class.

Automation Champion Approach (I-do):

To set the Created Date for an sObject in an Apex test class, you can use the Test.setCreatedDate(recordId, createdDatetime) method. This allows you to specify a desired date and time for the Created Date field of the sObject record within the test class.

Below are are details about the setCreatedDate parameters.

Parameters Name
Details
recordId It represent the ID of an sObject.
createdDatetime It represent value to assign to the sObject’s CreatedDate field.

Furthermore, it is important to note that the setCreatedDate method cannot be utilized within methods annotated with @isTest(SeeAllData=true). Let’s take a pause here, familiarize yourself with the setCreatedDate Apex test method.

Guided Practice (We-do):

Using these methods, you can effectively determine whether a Text field is null, empty, or blank in Apex. Let’s see this in action:

  1. Click on Setup | Developer Console. 
  2. To create an Apex test class, click on the File | New | Apex Class.
    
    
    @isTest 
    private class LeadTrigger_Test {
        static testMethod void testSetCreatedDate() {
            
            Lead l = new Lead(LastName='myLastname', Company='myCompany');
            insert l;
            Test.setCreatedDate(l.Id, Date.today().addDays(-1));
            Test.startTest();
            Lead myLead = [SELECT Id, FirstName, LastName, Company, CreatedDate FROM Lead 
                           WHERE Lastname ='myLastname' limit 1];
            myLead.FirstName = 'myFirstname';
            update myLead;
            System.assertEquals(myLead.CreatedDate, Date.today().addDays(-1));
            Test.stopTest();
        }
    }

  3. In the previous step, we Inserted the test record (1) before we set the CreatedDate (2) for test record.

Formative Assessment:

I want to hear from you!

What is one thing you learned from this post? How do you envision applying this new knowledge in the real world? Feel free to share in the comments below.

Have feedback, suggestions for posts, or need more information about Salesforce online training offered by me? Say hello, and leave a message!

Preferred Timing(required)

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Automation Champion

Subscribe now to keep reading and get access to the full archive.

Continue reading