Play Games

Search This Blog

Friday, September 2, 2022

System.SObjectException: SObject row was retrieved via SOQL without querying the requested field salesforce

Problem: This error generally occurs when you are using a field in the logic without quering that field in its related SOQL query.

Example:

Account a = [Select id,name from Account limit 1];

if(a != null) {

    if(a.rating =='Cold') {

        System.debug('Rating is Cold');

    }  

}

In the above code, line 3 rating field is referred but it's not queried in the soql query at line 1.

Updated Code:

Account a = [Select id,name,rating from Account limit 1];

if(a != null) {

    if(a.rating =='Cold') {

        System.debug('Rating is Cold');

    }  

}

Output:



No comments:

Post a Comment