You need to sign in to do that
Don't have an account?
Kunagu Varun Kumar Srinivasarao
Apex DML Selecting fields
I'm new to Apex and I stumbled upon this. I've a custom object named Cast which has a Rating field. I wanted to update all the Rating values to 3. I selected the Id field and updated all of them.
I've a similar problem in which I want to update birth date field of all records where birth date is > 2000. This is my code. However I'm getting an exception SObject row was retrieved via SOQL without querying the requested field:
If I include Birthdate__c field in my query, the error goes away.
I've a similar problem in which I want to update birth date field of all records where birth date is > 2000. This is my code. However I'm getting an exception SObject row was retrieved via SOQL without querying the requested field:
If I include Birthdate__c field in my query, the error goes away.
List<Person__c> listPersons = [SELECT Id FROM Person__c WHERE BirthDate__c > 2000-01-01]; for(Person__c person : listPersons){ person.BirthDate__c = person.BirthDate__c.addYears(-100); } try{ Update listPersons; } catch(DMLException ex){ System.debug(ex.getMessage()); }Why is that in my first code sample to update Rating field I was able to do that without selecting itin my query but why do I have to select Birthdate field for my second code?
Hi Varun,
This is because of the line inside the for-loop:
person.BirthDate__c = person.BirthDate__c.addYears(-100);
The operation includes person.BirthDate__c which needs to be queried well before.