2

I could see this in Apex developer guide saying:

The ID of an sObject is a read-only value and can never be modified explicitly in Apex unless it is cleared during a clone operation, or is assigned with a constructor.

This assigned with a constructor part is where i am confused. I could execute below code succesuflly:

Account A1 = new account(name = 'Acc1');
insert A1;
Account toUpdate = new account();
toUpdate.id = A1.id;

4th line does specify Id explicitly and not in constructor?

Where am i going wrong?

2
  • Are you updating/inserting the toUpdate account after that last line? It shouldn't let you assign the same ID to another account. Commented Jun 28, 2013 at 13:43
  • 1
    I think in your code there is missing a line #5: insert toUpdate;... and the error after that INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call. Commented Jun 28, 2013 at 13:52

1 Answer 1

3

This statement is no longer true. As of Spring '13 (version 27.0), the ID value is writable. Note that you must set your code to version 27.0 or higher to use this feature. If you write a class using version 26.0, you should still receive an unwritable field error. This is mentioned in the release notes:Setting ID Fields for Updates. Note also that the Execute Anonymous window always runs in the latest version of the API, so it is not possible to experiment using this window.

0

You must log in to answer this question.

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