+ Start a Discussion
George Laird 12George Laird 12 

Please help with a SOQL relationship query.

I'm braindead today and can't figure out the simplest of things.  

I have a custom object called VRNA__Policy__c.  
This object has a lookup relationship to ACCOUNT that's called VRNA__Issuing_Carrier__c.
I need to get the email address from the ACCOUNT lookup (VRNA__Issuing_Carrier__c) that's on the Policy record. 

This basic query works fine, but doesn't get the email address:
[SELECT VRNA__Issuing_Carrier__c FROM VRNA__Policy__c WHERE VRNA__Account__c =: Id AND RecordTypeID = '012f4000000giyZ']

I've tried:
[SELECT VRNA__Issuing_Carrier__c.email__c FROM VRNA__Policy__c WHERE VRNA__Account__c =: Id AND RecordTypeID = '012f4000000giyZ']

Shouldn't that work?  I'm getting an error that it doesn't understand the relationship.   
Best Answer chosen by George Laird 12
Syed Insha Jawaid 2Syed Insha Jawaid 2
Hi George

Update the query to : 
[SELECT VRNA__Issuing_Carrier__r.email__c FROM VRNA__Policy__c WHERE VRNA__Account__c =: Id AND RecordTypeID = '012f4000000giyZ']

Cheers!!!

All Answers

Syed Insha Jawaid 2Syed Insha Jawaid 2
Hi George

Update the query to : 
[SELECT VRNA__Issuing_Carrier__r.email__c FROM VRNA__Policy__c WHERE VRNA__Account__c =: Id AND RecordTypeID = '012f4000000giyZ']

Cheers!!!
This was selected as the best answer
Raj VakatiRaj Vakati
You need to use __r with relationship name
 
[SELECT VRNA__Issuing_Carrier__r.email__c FROM VRNA__Policy__c WHERE VRNA__Account__c =:Id AND RecordTypeID = '012f4000000giyZ']

 
George Laird 12George Laird 12
@Syed Insha Jawaid 2
@Raj Vakati

THANKS GUYS!  I told you I was brain dead today.  So easy.   Thanks so much!
George Laird 12George Laird 12
@Syed Insha Jawaid 2
@Raj Vakati

Hey guys, one problem here.  I did that but in my debug log I'm not getting the email as a result:

 List<VRNA__Policy__c> carriers = [SELECT VRNA__Issuing_Carrier__r.VRNA__Email__c  
 FROM VRNA__Policy__c
 WHERE VRNA__Account__c =: Id AND RecordTypeID = '012f4000000giyZ'];
 
  system.debug('Carriers: '+carriers);


My debug log looks like this:
     
13:54:43:029 USER_DEBUG [23]|DEBUG|Carriers: (VRNA__Policy__c:{VRNA__Issuing_Carrier__c=001M0000013E31FIAS, Id=a0mM0000002hYiwIAE, RecordTypeId=012f4000000giyZAAQ}, VRNA__Policy__c:{VRNA__Issuing_Carrier__c=001M0000013E9qnIAC, Id=a0mM0000002hYktIAE, RecordTypeId=012f4000000giyZAAQ})


Where's the email address?
Syed Insha Jawaid 2Syed Insha Jawaid 2
Hi George

Check out debug log using : 

system.debug('Carrier Email ' + carriers[0].VRNA__Issuing_Carrier__r.VRNA__Email__c);

Cheers!!!