[SalesForce] Sobject Constructor and ID

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?

Best Answer

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.

Related Topic