function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
billg3billg3 

SOQL to get followed opportunities

I am trying to write an SOQL query to get followed opportunities but I get strange errors each time.

 

For example the following works:

  SELECT ParentId FROM EntitySubscriptions WHERE ParentId IN

    (SELECT ID FROM Opportunitiy) LIMIT 1000

 

but it returns me ID's which I would then need to make additional calls to get the opportunities

 

If I try the reverse:

  SELECT Id, Name, CloseDate FROM Opportunity WHERE Id IN

    (SELECT ParentId FROM EntitySubscriptions LIMIT 1000)

 

then I get complaints when I include the limit (expected right parenthesis) or I get complaints for not including the LIMIT.

 

Any ideas what I am doing wrong?

 

Thanks, Bill

DharshniDharshni

Hi Bill,

 

1) Is the ParentId field in EntitySubscriptions references only to opportunity? In that case I think the following query will suffice.

 

SELECT parentID FROM EntitySubscriptions

 

This will return all the parent Opportunities for each subscription, from which you can filter out the distinct ones using sets.

 

2) If the ParentID field references to many objects (similar to whatID,whoID of Task), you can use Opportunity's keyPrefix(006) to filter records.