1

I have two custom objects ObjA and ObjB. ObjA.b is a LookUp field for ObjB. I have written following SOQL.

SELECT Id, b.Name FROM ObjA

This query gives correct results. Then I need to use these data in APEX code. But What is the return type of above query?

I know I can do as follows if there is only one object.

List<ObjA> obj_As = [SELECT Id, b FROM ObjA]

2 Answers 2

1

Your type returned from a query is always defined by your FROM clause. You can sometimes assign to a singular object instead of a list when you know there is exactly one result. But the type would be the same. No other clause in your query matters. Only FROM.

Please note that the syntax for querying a parent field involves using the relationship name. For example, if you create a custom lookup named Parent__c, you would typically pull Parent__r.Name. If you use some arbitrary string like b, you are actually just using an alias on the query object itself.

3
  • But then I can not access obj_As[0].b.Name Commented Oct 17, 2019 at 3:58
  • 1
    Yes you can...why do you think you cannot?
    – Adrian Larson
    Commented Oct 17, 2019 at 4:07
  • Yeah. It's working... Commented Oct 17, 2019 at 23:44
0

List obj_As = [SELECT Id, ObjA__r.b FROM ObjA] you need to specify relationship between your object and fields for that you must use __r for relation in SOQL

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .