2

I am new to Salesforce and currently trying to query the fields from the Store object related to the OrderSummary standard object. Store has a lookup relationship with OrderSummary object.

First, I tried to do a normal child->parent SOQL query but received below error

Select Id, SalesStore.Description from OrderSummary where Id = 'MY_ID'

No such column 'Description' on entity 'Name'

Upon some research I found that this maybe because the field is polymorphic. I ran below code and the result of method isNamePointing is true.

System.debug(OrderSummary.SalesStoreId.getDescribe().isNamePointing());

Then I tried using the TYPEOF clause as below but got another error.

Select Id, TypeOf SalesStore When Store Then Description END from OrderSummary where Id = '1Os7a0000004DWSCA2'

sObject type 'Store' is not supported.

I also tried another variant of TYPEOF clause as below but got another error

Select Id, TypeOf SalesStoreId When Store Then Description END from OrderSummary where Id = '1Os7a0000004DWSCA2'

No such relation 'SalesStoreId' on entity 'OrderSummary'.

So, how can I query the fields of the Store object associated with the OrderSummary object.

Data Model

enter image description here

enter image description here

enter image description here

4
  • try Select Id, TypeOf SalesStoreId When WebStore Then Description END from OrderSummary where Id = 'MY_ID'
    – cropredy
    Commented Nov 27, 2022 at 17:34
  • @cropredy: I tried that and the error I received is No such relation 'SalesStoreId' on entity 'OrderSummary'.
    – RanRag
    Commented Nov 27, 2022 at 18:59
  • my bad, try Select Id, TypeOf SalesStore When WebStore Then Description END from OrderSummary where Id = 'MY_ID'
    – cropredy
    Commented Nov 28, 2022 at 18:18
  • @cropredy: thank you. The query executed fine i.e. without any error but I do not get any fields related to WebStore object back. See screenshot postimg.cc/jCCLZfhD. I tried with different fields as well but similar result. To my surprise, the column for field Description itself is missing.
    – RanRag
    Commented Nov 29, 2022 at 10:47

2 Answers 2

2

Use this query

SELECT Id, TypeOf SalesStore WHEN WebStore THEN Description END 
 FROM OrderSummary WHERE Id = 'MY_ID'

The TypeOf must name the relationship name (which is SalesStore)

The reason you don't see the result in Developer Console (DC) is a limitation of the query editor in DC.

If you change the query in DC to:

SELECT Id, TypeOf SalesStore WHEN WebStore THEN Description END 
 FROM OrderSummary WHERE Id = 'MY_ID' AND SalesStore.Type = 'WebStore'

then the DC will show a column called SalesStore with a value [object Object] that you can't inspect. However, if you use Apex for the first SOQL, you'll be able to debug/use the value of the SalesStore relationship

If you use a modern IDE (I used Illuminated Cloud), the first query will display the value of SalesStore.Description in the IDE's query tool.

1
  • Thank you! works like a charm
    – RanRag
    Commented Dec 1, 2022 at 13:35
-3

Try:

Select Id, SalesStoreId.Description from OrderSummary where Id = 'MY_ID'

looks like the API name of the field is SalesStoreId and not SalesStore

3
  • ?? relationship names are not suffixed by Id, the doc says the relationship name is SalesStore
    – cropredy
    Commented Nov 27, 2022 at 17:33
  • You are right, need to use the Relation name. however, sometime for custom fields you need to use the name of the lookup field by changing the __c for __r. But I don't think this is the case
    – Jane B.
    Commented Nov 27, 2022 at 18:39
  • N.B. always (not sometimes) for custom lookup fields you use __r
    – cropredy
    Commented Nov 29, 2022 at 18:30

You must log in to answer this question.

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