0

As per my understanding, the below code should throw System.SObjectException: SObject row was retrieved via SOQL without querying the requested field, but instead it updated the account phone without any exception can someone please help me to understand the same?

public static void SObjectException1() {

    Account act = [SELECT ID FROM Account WHERE Name='GenePoint' LIMIT 1];
    act.phone = '1234567810';
    update act;
}

is it a best practice to not query the fields which we are going to update?

1 Answer 1

4

You can set any fields you want on queried objects. What you cannot do is get their values. This code would throw the error you expect:

Account record= [SELECT Id FROM Account WHERE Name='GenePoint' LIMIT 1];
system.debug(record.Phone);
0

You must log in to answer this question.

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