1

Here is my SOQL query below in Alteryx application where I have to extract the Id from Contact object which has two level sub-selects. I could only make the SOQL query to work for 1 level of subselect but not more.

Are there any workarounds for this? It would be of great help if someone can edit my code to make it work.

Select Id from Contact c
Where
c.Id in
(SELECT 
contact__c
FROM 
VIN__c  v
where v.Id in
(SELECT 
vin__c
FROM 
Mileage_Reading__c
WHERE 
CreatedDate >= 2020-06-01T00:00:00Z AND  CreatedDate < 2020-07-01T00:00:00Z))

1 Answer 1

0

You cannot go a level deep in NESTED Subquery in SOQL. This is documented here

You cannot nest a semi-join or anti-join statement in another semi-join or anti-join statement.

You can use semi-joins and anti-joins in the main WHERE statement, but not in a subquery WHERE statement.

You can split the above queries into multiple. In Salesforce apex language, this is how you do,

List<Mileage_Reading__c>  lstMileageReading = [SELECT vin__c FROM Mileage_Reading__c WHERE CreatedDate >= 2020-06-01T00:00:00Z AND  CreatedDate < 2020-07-01T00:00:00Z];

List<Contact> lstcontact = [Select Id FROM Contact c
  WHERE
  c.Id in
 (SELECT 
  contact__c
  FROM 
  VIN__c  v
  WHERE v.Id IN :lstMileageReading)]:

If you need this from a third party REST Service, you can create an apex class that exposes this as a REST Utility. Then you can invoke the apex rest service as an API.

Alteryx Connector

You can a Select tools and Join tool to acheive this.

7
  • I have tried using your code. It gives me this error, "unexpected token: List"
    – mysticmind
    Commented Aug 23, 2020 at 22:51
  • where are you trying? Dev Console apex anonymous window? Commented Aug 23, 2020 at 22:52
  • Its an apex code and not completely SOQL. Are you using this SOQL in apex or from any other system invoking salesforce REST API? Commented Aug 23, 2020 at 22:53
  • I am using Salesforce connector in Alteryx. I have querying data from prod enviroment with read only access.
    – mysticmind
    Commented Aug 23, 2020 at 22:54
  • 1
    You can use two Select tool and a Join tool in Alteryx to join them together. Commented Aug 24, 2020 at 0:15

You must log in to answer this question.

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