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

  • Manpreet

    Member
    May 2, 2017 at 1:36 pm

    Unit testing code which has logic around the CreatedDate

  • 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.

Viewing 1 - 3 of 3 posts

Log In to reply.

Popular Salesforce Blogs

Steps to Connect Pardot to Salesforce

Steps to Connect Pardot to Salesforce

Blog in

Steps to Integrate Salesforce and Pardot: There are some steps to install Pardot in salesforce 1- Click on this link https://login.salesforce.com/?ec=302&startURL=%2Fpackaging%2FinstallPackage.apexp%3Fp0%3D04t1W000000U7P8(production) https://test.salesforce.com/?ec=302&startURL=%2Fpackaging%2FinstallPackage.apexp%3Fp0%3D04t1W000000U7P8 (sandbox) 2- After…

Popular Salesforce Videos