Covered Product is an object and Installed product is a lookup field on Covered product and Status is a picklist field on Installed product.
When I am trying to run the below code in Execute Anonymous window then I am getting error : Line: 4, Column: 1 System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Service_Contract_Products__c.Installed_Product__r. But when I am running the same code inside a class I am not getting the error.
Can someone please provide the solution how to check the status of Installed product in Covered Product object? And why I am not getting error inside class but getting error in Execute Anonymous window?
List<Service_Contract_Products__c> coveredProducts = [SELECT Id,Installed_Product__c FROM Service_Contract_Products__c];
for(Service_Contract_Products__c coverProd : coveredProducts)
{
if(coverProd.Installed_Product__r.Status__c != 'OPER - Operational')
{
System.debug(coverProd.Installed_Product__r.Status__c);
}
}