1

I am trying to insert a list of records with a CreatedDate of my choosing. I read the documentation about what settings are needed to grant access to insert values for the audit fields.

I have enabled the user interface setting "“Set Audit Fields upon Record Creation” and “Update Records with Inactive Owners” User Permissions" and created a permission set that grants me access.

https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/system_fields.htm

However, I am still getting the error: "Field is not writeable: Account.CreatedDate".

Example script below:

List<Account> createddatelist =
[SELECT name, createddate FROM Account WHERE id = '001a000001kXUZT'];

System.debug('createdatelist');

DateTime testdate = DateTime.newInstance(1990, 3, 17);

for(Account acct : createddatelist){

acct.createddate = testdate;
acct.name = 'test of date';

}

insert createddatelist;
  • 1
    Mercury86.. have you try to execute this code from dev console is it working ? – Ratan Paul Apr 7 '16 at 15:27
  • @sfdcweb Yes, that is were the error is coming from. Mark Keats has answered this question. The documentation above mentions that I can only do it via the API. I still find it strange that I cannot do so via the dev console, but per the documentation Mark Keats is correct. – Steel Reserve 211 Apr 7 '16 at 15:31
  • 1
    Mercury86.... Just now I have tested in my org. I am creating Account record with createdDate 1990 in my apex class it is working... is your permission set set audit checkbox is enabled ? – Ratan Paul Apr 7 '16 at 15:33
  • @sfdcweb I just tried again now, still the same error. Data Loader did work tho. – Steel Reserve 211 Apr 7 '16 at 15:43
  • @AllabouttheW: I don't see Created date field in mapping of data loader? – Gurmeet singh Sep 20 '19 at 16:27
6

The 'Set Audit Fields upon Record Creation' feature only applied to records created using the API, not from Apex code.

| improve this answer | |
  • Ah shoot, your right!. It says right there on the documentation. – Steel Reserve 211 Apr 7 '16 at 15:24
  • 1
    Yes. The data loader would work fine. – Mark Keats Apr 7 '16 at 15:26
  • 1
    @MarkKeats when we try to create Account from dev console? is it an API call ? – Ratan Paul Apr 7 '16 at 15:29
  • @MarkKeats I just now tried in my org.. I am able to create account from apex code and I also tried to create from dev console. in both scenario it is creating Account. – Ratan Paul Apr 7 '16 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.