0

I'm trying to generate a set of Opportunity Ids that have 'Legal' cases. But I'm getting the 'inner select field 'Opportunity__r.Id' cannot have more than one level of relationships. Is there a better way of doing this without first pulling a list of sObjects then iterating using a for loop to get the Ids? Thank for helping out!

Set<Id> oppIds = new Map<Id, Opportunity>([SELECT Id FROM Opportunity WHERE     Id IN (SELECT Opportunity__r.Id FROM Case WHERE RecordType.Name = 'Legal')]);

1 Answer 1

4

Change __r.Id to __c. That way it does not resolve to an object but just an Id.

Set<Id> oppIds = new Map<Id, Opportunity>([
    SELECT Id FROM Opportunity
    WHERE Id IN (
        SELECT Opportunity__c
        FROM Case
        WHERE RecordType.Name = 'Legal'
    )
]).keySet().clone();
2
  • Thanks Adrian. Tried it but I'm getting an 'Illegal assignment' error from Map to Set.
    – humanoid
    Commented Jan 29, 2015 at 21:58
  • You should also tweak the query to use RecordType.DeveloperName, which is unique.
    – Adrian Larson
    Commented Jan 29, 2015 at 22:34

You must log in to answer this question.

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