3

I am trying to execute this SOQL:

productIdsThatExist = [SELECT id 
                      FROM Product2 
                      WHERE id IN (SELECT id 
                                   FROM Cross_sell__c 
                                   WHERE (Accountforproduct__c =: acc.id) 
                                   AND (Discussion_status__c!=null)
                                   )];

I get this error:

Error: Compile Error: The selected field 'id' in the subquery and the left operand field in the where expression in the outer query 'id' should point to the same object type at line 64 column 30  

My data model is:

Accounts > MD > Cross_sell__c < Lookup < Products

Which seems to be exactly the same as in Salesforce's deeper look at SOQL and Relationship Queries.

2 Answers 2

5

The example from Salesforce's deeper look at SOQL and Relationship Queries is this:

SELECT Name FROM Position_c WHERE Id NOT IN
(SELECT Position__c FROM Job_Application__c)

Note that the sub-query is selecting the id (Position__c) to the Position record. You will need to do the same thing. You should be selecting the field that has the Id to Cross_sell__c. You are currently are comparing a Product2 Id to a Cross_sell__c Id. You SOQL should be something like this:

productIdsThatExist = [SELECT id 
                      FROM Product2 
                      WHERE Cross_sell__c IN (SELECT id 
                                   FROM Cross_sell__c 
                                   WHERE (Accountforproduct__c =: acc.id) 
                                   AND (Discussion_status__c!=null)
                                   )];
1
  • Ahhh. Got it. Seems so obvious now. I think their example wasn't very good! Thanks for help.
    – dzh
    Commented Mar 11, 2014 at 13:33
2

Try this. In your outer WHERE clause put the correct reference API name for Cross_sell__c

productIdsThatExist = [SELECT id 
                          FROM Product2 
                          WHERE Cross_sell__c IN (SELECT id 
                                       FROM Cross_sell__c 
                                       WHERE (Accountforproduct__c =: acc.id) 
                                       AND (Discussion_status__c!=null)
                                       )];
1

You must log in to answer this question.

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