4

This is a Follow up question to this. What I am trying to do is set the createdDate field in Account record using the below code as suggested by cropredy. I have enabled Enable "Set Audit Fields upon Record Creation" and "Update Records with Inactive Owners User Permissions". Here is my code.

Account acc = (Account) Json.deserialize('{"createdDate" : "2016-01-01T00:00:00Z","Name":"TempAccount"}',Account.class);
insert acc;
acc = [Select id, name, createdDate from Account where id =: acc.id];
System.debug(acc.id + '' + acc.CreatedDate);

3 Answers 3

4

I forgot, in V36.0, there's a new method in the Test class. Create the sObject and then use the method Test.setCreatedDate(..) on the ID of the sobject that needs a createdDate other than now. You have to do this before Test.startTest() From the doc:

public static void setCreatedDate(Id recordId, Datetime createdDatetime)


@isTest 
private class SetCreatedDateTest {
    static testMethod void testSetCreatedDate() {
        Account a = new Account(name='myAccount');
        insert a;
        Test.setCreatedDate(a.Id, DateTime.newInstance(2012,12,12));
        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();
    }
}

Note - outstanding issue using this on Note, hopefully resolved by time someone looks at this answer in the future (Summer 16+).

2

The "Set Audit Fields upon Record Creation" permission applies only to the API (being called via SOAP/REST from an external tool), cf. https://help.salesforce.com/apex/HTViewSolution?urlname=Enabling-Create-Audit-Fields&language=en_US The reasoning I think being that you want to be able to set those dates when you migrate from other systems, but not in other cases.

You are however trying to set the CreatedDate from within APEX code rather than via the API. So here, the permission doesn't apply and there is no alternative permission to my knowledge to allow you to set the CreatedDate from within APEX. I guess what you could do is make a REST/SOAP call from the APEX code to your own org... but it is a crooked solution.

1

I'm not sure with which release this has been enabled but you can now set audit fields with Apex too.

For overriding audit fields in Apex Classes:

Setup -> User Interface: Enable "Set Audit Fields upon Record Creation" and "Update Records with Inactive Owners" User Permissions

For Anonymous Apex code the context User should have Set Audit Fields upon Record Creation / Update Records with Inactive Owners (one or both - depending on what's your intention) permissions enabled as well (via Profile or Permission Set).

What's interesting here, you can turn the Enable[...] flag off in User Interface settings while still having a code which overrides audit fields - it will just not work anymore (ex. CreatedDate will be set to the real one, not "fake")

4
  • 1
    There is a big confusion here. You still CANNOT set audit fields on record create from apex code, i.e. classes, triggers, batches etc. Execute Anonymous window is a special case - it's just a text editor which communicates with SF org via Tooling API. That's why it works there - because technically that's via API, but not from internal apex code. Life is tricky, ha)
    – wesaw
    Feb 11, 2021 at 14:01
  • @wesaw I'm trying to set CreatedDate on Knowledge__kav records and I get confusing results. I start a batchable apex class via Database.execute() from the developer console. The batchable attempts to insert a new Knowledge__kav with a createddate, but it gets overriden with the current datetime. The before insert trigger of the knowledge__kav record, reveals the createddate to be null. Now for the confusion: When I switch out the code and insert an account instead, it works fine. CreatedDate gets set to the one given in the apex code.
    – Andreas86
    May 5, 2021 at 11:46
  • The same seems to be the case if I use the API to load the data. Account works, Knowledge__kav doesnt
    – Andreas86
    May 5, 2021 at 11:59
  • @wesaw I can't find anything in the SF docs that says you must use the API or anonymous Apex to be able to set CreatedDate. I tried it in a Trigger in my Summer 23 scratch org and it works there. Sep 21, 2023 at 9:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .