I read some other questions but I'm still having trouble.

I' ve got Custom object A and B. A has got a LookUp relationship field on B named LookUpB__c.

I'd like to get the value of Name of B. This is the query I'm using:

SELECT Name FROM B WHERE id =: A.LookUpB__c

What am I doing wrong?

  • 1
    So are you looking for a particular record or just all the names of B? The way your query is set up right now (when fixed) will just grab all the names of B
    – Griffin
    Jul 22 '16 at 16:00

// Assuming you are querying object A , need to use (__r) to retrieve Parent fields.

__r is for Custom objects reference For example: Custom_Object__r . It is used for reference custom object relationship name in Apex or visualforce page, formula field etc. Used as suffix.

String strBName = [SELECT Id, LookUpB__c, LookUpB__r.Name FROM A WHERE Id=: 'Id of Record A' limit 1].LookUpB__r.Name;

If you're looking to just grab the Name of Object B when given Object A you should try this:

SELECT Id, LookUpB__c.Name FROM A WHERE Id= [Id of Object A]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.