What is retrieved via SOQL without querying the requested field?

What is 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.

What happens when a user is trying to access a field that was not selected in the SOQL query?

If you try to access a field that was not selected in the SOQL or SOSL query (other than ID), you receive a runtime error, even if the field contains a value in the database. Consequently, you must dereference the field in order to access it.

How many records can be retrieved by SOQL query?

The total number of records that can be returned by SOQL queries in a request is 50,000. If returning a large set of queries causes you to exceed your heap limit, then a SOQL query for loop must be used instead. It can process multiple batches of records through the use of internal calls to query and queryMore.

How do you handle list has no rows for assignment to sObject?

The error “List has no rows for assignment to SObject” occurs when query doesn’t return any rows. While a SELECT normally returns an array/list, these statements are using the shorthand syntax that assumes only one row is returned.

How many SOSL records can be returned by SOSL query?

2,000 results
LIMIT is an optional clause that can be added to a SOSL query to specify the maximum number of rows that are returned in the text query, up to 2,000 results. If unspecified, the default is the maximum 2,000 results.

What is newMap in trigger?

Trigger.newMap: Trigger.newMap is a map with key as ID of the record and value as the record itself. Just like the above explanation, in case of accounts when we say trigger.newMap we are talking about a map of key-value pairs where the key is the account ID and the value is the account record itself.

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 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 ?

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 query off of a junction object?

If you’re querying off of a junction object, you would need to query the relationship using a subquery. You can check the Technology or AccountTechnologies object definition (App Setup > Create > Objects) and click the Master-Detail field to find the Child Relationship Name.