I'm trying to get a list, mlcs, of a custom objects "my last call date", which has a related contact, and add all of these contacts to a list of contacts, cs.
To use the .add() method, I need to use the contact__r suffix for the lookup field, Contact__c, or else I get an error. But, I have to query this field using the contact__c suffix, or else it will tell me that no row was found for contact__r. But if I do this, then it says I didn't query contact__r and cannot use it.
apex:
public class MyLastCallListView2{
public String currentUser = UserInfo.getUserId();
public list <My_Last_Call__c> mlcs {get;set;}
public list <contact> cs {get;set;}
public void search(){
string callsList='select name,id, Contact__c, Contact_name__c, User__c from My_Last_Call__c WHERE User__c=\''+currentUser+'\'';
system.debug(callsList);
mlcs= Database.query(callsList);
for (My_Last_Call__c m : mlcs){
cs.add(m.Contact__r);
}
public MyLastCallListView2(ApexPages.StandardController controller){}
}
Error:
SObject row was retrieved via SOQL without querying the requested field: My_Last_Call__c.Contact__r
If I change cs.add(m.Contact__r);
to cs.add(m.Contact__c);
or cs.add(m.Contact__c.id);
, the code will not compile, with this error: Error: Compile Error: Incompatible element type Id for collection of Contact at line 22 column 2
My_Last_Call__c
?