Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

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)
                                   )];
share|improve this answer
    
Ahhh. Got it. Seems so obvious now. I think their example wasn't very good! Thanks for help. –  dzh Mar 11 at 13:33

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)
                                       )];
share|improve this answer
    
could you able to resolve the issue? if you can't find the Lookup API Name, check it in the WSDL success.salesforce.com/answers?id=90630000000gx2mAAA –  highfive Mar 11 at 12:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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