0

As pointed out in other questions some fields are queried in APEX even if not mentioned explicitly, including Id field. I can remember these fields could be used only for reading data, not for actually updating records, meaning you had to explicitly query Id field to update a record, but you could read Id value without querying it.

So code like this would throw an error:

Account acc = [SELECT Name FROM Account Limit 1];
acc.Name = 'Test';
update acc; // Error - SObject row was retrieved via SOQL without... or something like that

But code like this would not throw an error:

Account acc = [SELECT Name FROM Account Limit 1];
System.debug(acc.Id);

But it is no longer the case. Or it never was? Help me with this I'm losing mind over this!

1 Answer 1

2

Even the first snippet of code will not throw an error because you are updating the record and we just need id of the record for updating and you can update any field in the Account irrespective of the field you queried.

Like below.

Account acc = [SELECT Name FROM Account Limit 1];
acc.description='sample desciption';
update acc;

But if you want to access the same description field then you will receive the error saying System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Account.Description.

Account acc = [SELECT Name FROM Account Limit 1];
system.debug( 'Account Description'+acc.Description);

As mentioned in your post record Id is not required and it is queried by default.

You must log in to answer this question.

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