+ Start a Discussion
Ian QuahIan Quah 

cant use static reference for sobject fields

If I have some object A, that has fields
Name(string), B__c (sObject) and C__c (sObject)

B__c has the fields 
Name(string), BB__c (string of "Yes", "No" and "Maybe)

and C__c has the fields 
Name(string), CC_c (bool)

how would I filter such that I can get all A's where 
A.B__c.BB__c == "Yes" and A.C__c.CC__c == true

I basically tried the following but I got an error about sObject
return [
            SELECT Id, Name, B__c, C__c
            FROM A
            WHERE B__c.BB__c = :"Yes"
            And C__c.CC__c = :true

Best Answer chosen by Ian Quah
Abhishek BansalAbhishek Bansal
Hi Ian,

There are couple of things being wrong in your code:
  1. For relationship field we should use "__r" in place of "__c".
  2. : is used for binding variables. So we should avoid : when we are directly comparing the values.
Your code should be modifed as below:
return [
            SELECT Id, Name, B__c, C__c
            FROM A
            WHERE B__r.BB__c = "Yes"
            And C__r.CC__c = true

Please let me know if you need any further help on this.

Abhishek Bansal.