+ Start a Discussion
anup ramanup ram 

Lookup field in where clause of SOQL not returning values

Hi All,

I have a lookup field on a object. I have written a soql to return name and other fields from the object with the looku field in the where clause. But this is not returning me values.

\please help.

 

list<contact> con = [select id,name from contact where name like 'a%'];
integer consize = con.size();     // returning 1, ( correct )
system.debug(' con size filter ' + consize);
for (contact conn : con)
{
system.debug(' contact name ' + conn.name ); // returning correct name

list<class__c> cls = [select name__c,Fee__c from class__c where classteacher__c =:conn.name];

integer classsize = cls.size();   // showing 0 , but one class is available with class teacher of conn.name
system.debug(' class size ' + classsize);

 

here classteacher__c is the lookup field to contact.

there is one class with classteacher name which is retuerned by conn.name but it is not being fetched.

Please help.

 

 

Devendra@SFDCDevendra@SFDC

Hi,

 

I am assuming tha Classteacher__c is relationship with Contact. The relationships are identified using Ids.

 

So Instead of Name, you need to use contact Id.

 

list<class__c> cls = [select name__c,Fee__c from class__c where classteacher__c =:conn.Id];

 

One more thing, you have written the above line inside a for loop, which can hit Apex Governor Limit.

 

Therefore avoid wrtting a query inside a for loop.

 

 

 

 

 

 

 

anup ramanup ram

Thanks it solved my problem.

 

Can u tell  me how i can reframe my logic to elimiate soql inside for loop. What is the best possible way.