What to do if sobject was not retrieved via soql?

What to do if sobject was not retrieved via soql?

Usually if apex code attempts to access an sObject field that wasn’t retrieved via SOQL I get an exception like: This if useful, as it tells me to go back to the SOQL query that retrieved the Opportunity and add the CustomField__c field to the select.

How is a sobject row retrieved in apex?

SObject row was retrieved via SOQL without querying the requested field. SObject row was retrieved via SOQL without querying the requested field is very common error in apex. As name of error suggest, this error occurs when we try to use some field of an object instance which is not queried. There can be two possible reason for this error.

Why is sobject row retrieved without querying the requested field?

SObject row was retrieved via SOQL without querying the requested field is very common error in apex. As name of error suggest, this error occurs when we try to use some field of an object instance which is not queried. There can be two possible reason for this error. When we try to use a field in Apex or visualforce which is not queried.

Can a sobject row be passed by reference?

The only caveat would be that since sObjects are passed by reference, hopefully there isn’t a sneaky bit of assignment which is setting the value of CustomField1 somewhere in the foodchain. If there is an assignment of value then it wouldn’t throw the exception for any subsequent access.

How to retrieve sobject row without querying the requested field?

So, in other words, if I retrieve Status__c in the query I’ll just get that same sObject error for Contract_Start_Date__c and so on and so on… I’m sure there’s gotta be a better way than this. Anybody know them ?

How to avoid system.sobjectexception in YPU?

You can add OpportunityId in query while query..you have not queried this fields and you are using so ypu are getting this error.. Please check and let me know if it helps..To avoid these kind of error you should always query all the fields which we are using or reffering in code ….