[SalesForce] API name of ‘Location’ field in hierarchy custom setting or how to get it in code

I want to fetch type of hierarchy setting(profile OR User), compare it and do some logic. For that I am using setupOwner.Type in SOQL query as:

SELECT SetupOwner.Name,SetupOwner.Type,SetupOwnerId, Field1__c,
            FROM MyUserSettings__c
            ORDER BY SetupOwner.Name

It is giving me output as:

> SetupOwner.Type for User: 'User'
> SetupOwner.Type for profile: '00e'
> SetupOwner.Type for org: '00D'

But when I use where clause:

 where SetupOwner.Type='00e'

it gives me 0 results.

How can we obtain records where type is 'Profile'? Or what is the API name of field 'Location' in hierarchy settings?

Also, I could not find official documentation for 'SetupOwner'. Is there any?

Best Answer

That is not a custom setting feature, you are referring to a polymorphic field, refer Cross-Object Owner Fields.

Now you can add filter after querying:

for(OverrideStandardButtons__c setting: [SELECT Name, 
    SetupOwnerId FROM OverrideStandardButtons__c]) {
    if(setting.SetupOwnerId.getSObjectType() == User.SObjectType ||
        setting.SetupOwnerId.getSObjectType() == Profile.SObjectType) {
        System.debug('ownerType: ' + setting.Name);
    }
}

Credit goes to sfdcfox!


Added-

For your example, simpler would be to use following query:

SELECT Name, SetupOwnerId, SystemModstamp FROM OverrideStandardButtons__c WHERE SetupOwnerId NOT IN (SELECT Id FROM Organization)