We already knew that Account
and Contact
have relationship where as Account is parent. I created one more child for Account called RelateObj
. But there is no any relation between RelateObj
and Contact
.
Now, I want to get Account
and Contact
records with respect to RelateObj
. I tried with this by using Apex collection and it gives this error:
SObject row was retrieved via SOQL without querying the requested field: Contact.Account` and the class is listed below.
public class Example {
public static List<childtoacc__c> findSiblings() {
List<Contact> children = [select id,LastName from contact];
List<RelateObj__c> values = new List<RelateObj__c>();
Set<Id> parentIds = new Set<Id>();
for (Contact child : children) {
parentIds.add(child.Account.Id);
values = [SELECT Id,name FROM RelateObj__c WHERE Id IN :parentIds];
}
System.debug('Values:::'+values);
return values;
}
}
Why do I get this error? Can we achieve this functionality only using SOQL?