Activity Forums Salesforce® Discussions Unit testing code which has logic around the CreatedDate in Salesforce

  • Suraj

    Member
    May 2, 2017 at 1:40 pm

    Hi Manpreet,

    You can create sObjects in memory with arbitrary CreatedDate values by using JSON.deserialize. This doesn’t enforce the normal read-only field attributes that prevent you from setting a createdDate value. However you can’t commit arbitrary CreatedDate values to the database (or else it would be a serious security issue).

    An example of doing so :

    String caseJSON = ‘{“attributes”:{“type”:”Case”,”url”:”/services/data/v25.0/sobjects/Case/500E0000002nH2fIAE”},

    “Id”:”500E0000002nH2fIAE”,

    “CreatedDate”:”2012-10-04T17:54:26.000+0000″}’;
    Case c = (Case) JSON.deserialize(caseJSON, Case.class );
    System.debug(c.createdDate);

  • Avnish Yadav

    Member
    September 30, 2018 at 3:19 am

    Hello,

    Use this-

    Test.setCreatedDate(recordId, createdDatetime)

    Sets CreatedDate for a test-context sObject.

    Thanks.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos