This is my first polymorphic query so I could be missing something quite simple here, but is there another way to write this without using the TYPEOF expression? I'm getting the error that "TYPEOF expressions are not supported in this organisation".

Query below:

list<oppSector__c> bSectorList = new list<oppSector__c>();

  bSectorList = [SELECT Id, Opp__c, SecNum__c,
                    TYPEOF What 
                        WHEN Opportunity THEN Id
                FROM oppSector__c WHERE Opp__c = :stackFlightIDSet];

        system.debug('***************Created List*******' + bSectorList.size());

Is this just a matter of getting TYPEOF enabled for the org? Or is there another way of writing this without using the TYPEOF? Opp__c is the relationship field to Opportunity. oppSector__c is the child/detail object. I've tried lots of iterations based on the documentation here but can't seem to return any results in the debug log

  • Error is correct. TYPEOF is not supported in SOQL. What are you trying to so? are you trying yo select all oppSector__c which meets your criteria (Opp__c = :stackFlightIDSet) and oppSector__c related to Opportunity ? May 6, 2015 at 6:02

1 Answer 1


Assuming that your stackFlightIDSet variable is a list of Opportunity ids, You could just query your oppSector__c object directly, ie:

list<oppSector__c> bSectorList = new list<oppSector__c>();
bSectorList = [SELECT Id, Opp__c, SecNum__c
               FROM oppSector__c WHERE Opp__c = :stackFlightIDSet];

If this is not what you want, you can also do a subselect from parent Opportunity object.

This would look like:

list<Opportunity> opportunities = new list<Opportunity>();
opportunities = [SELECT Id, Name,
                     (SELECT Id, Opp__c, SecNum__c
                      FROM oppSectors__c)
                 FROM Opportunity WHERE Id = :stackFlightIDSet];

Note the name of the child relationship - it's oppSectors__c in my example, but you should check this on the lookup field definition.

Also, when you want to access the sub select object, just treat them like a standard list - ie opportunities[0].oppSectors__c[0]

  • Thanks @CaspNZ that first one works nicely. I could've sworn I had something like that to start with but was getting an error that the Opportunity object wasn't accessible.
    – Greg J
    May 6, 2015 at 7:08

You must log in to answer this question.

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