1

This is my SOQL query

List<BOG_Committee_w_Membership__c> bogQueryList = [
    SELECT 
        BOG_Committee__r.Name, 
        BOG_Committee__r.Official_1__r.Name, 
        BOG_Committee__r.Role_1__c, 
        BOG_Membership__r.ORG__c, 
        Member_Type__c,
        BOG_Membership__r.Contact__r.Name, 
        BOG_Membership__r.BOG_Membership_Status__c  
    FROM BOG_Committee_w_Membership__c 
    WHERE BOG_Committee__r.Name = 'Executive'
    AND BOG_Membership__r.BOG_Membership_Status__c = 'Active'
    AND BOG_Membership__r.RecordTypeID = '012w0000000MNtO'];

I get:

Invalid field BOG_Membership__r.BOG_Membership_Status__c for BOG_Committee_w_Membership__c

when I do this

for(BOG_Committee_w_Membership__c record :bogQueryList) 
{ 
    if ((String) record.get('BOG_Membership__r.BOG_Membership_Status__c') <> Null)

How to get that value from the result set? Also, is there a better way to do this?

Nick C
  • 6,565
  • 1
  • 29
  • 57
Apex N-u-b
  • 1,278
  • 1
  • 28
  • 51

1 Answers1

0

You should be able to get it by just using the following:

record.BOG_Membership__r.BOG_Membership_Status__c

i.e.

if(record.BOG_Membership__r.BOG_Membership_Status__c != null)
{
    .. do stuff ..
}

The reason the record.get('BOG_Membership__r.BOG_Membership_Status__c') doesn't work is because you need to use getSobject() to get the parent record when using the dynamic syntax.

Using the .get('fieldname') is a dynamic syntax.

i.e. you could have a variable defining which field to retrieve:

String myFieldName = '.. appropriate field name ..';

if(record.get(myFieldName) != null)
{   ..  do stuff .. }

If you wanted to use a dynamic field, you'd need to get the parent SObject first. For more information on this, see: Using sObject.get() to fetch a field value from a related parent object

Nick C
  • 6,565
  • 1
  • 29
  • 57
  • Please explain this. When I replaced record.get('BOG_Membership__r.BOG_Membership_Status__c') with record.BOG_Membership__r.BOG_Membership_Status__c the error went away. When to use each of them? – Apex N-u-b Sep 13 '16 at 11:56
  • @ApexN-u-b See updated answer for more information. Specifically, that link has some great info in it. – Nick C Sep 13 '16 at 12:32