0

I have a hierarchical custom setting from where I want to query the Profile Name and some fields from that setting. Is there any way I can profile name/user name from the custom setting... I've tried below query to return fields from the setting...

 SELECT TYPEOF SetupOwner
    WHEN Profile THEN Id, Name END
    Discount__c, Price__c
    FROM Hierarchy_Object__c
    WHERE SetupOwnerId IN (Select Id FROM Profile)

It is throwing an error says "SOQL TYPEOF expressions cannot have an alias"... Can anyone help me how to resolve the query.

Thanks in Advance...

2 Answers 2

0

There is a missing comma in the SOQL after END.

To include fieldList in the SOQL query with TYPEOF, you can either include the fields list after END :

 SELECT 
    TYPEOF SetupOwner
       WHEN Profile THEN Id, Name 
    END,
    Discount__c, Price__c
    FROM Hierarchy_Object__c
    WHERE SetupOwnerId IN (Select Id FROM Profile)

Or add them at the beginning :

 SELECT  Discount__c, Price__c,
    TYPEOF SetupOwner
      WHEN Profile THEN Id, Name 
    END
    FROM Hierarchy_Object__c
    WHERE SetupOwnerId IN (Select Id FROM Profile)

Refer to the documentation for the TYPEOF syntax

2
  • I have added the VF page as answer. Can you check and let me know what I missed....
    – user85512
    Jan 28, 2021 at 10:59
  • @SaiVineeth This is a different topic, please post it as a new question
    – Shamina
    Jan 28, 2021 at 11:04
0

It worked and didn't throw any error. I wanted to display Profile Name and the fields in a vf page. So I added the query results to a List. But the list returning no values...Here is my VF page...

    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockSection title="Profiles">
                <apex:pageBlockTable value="{!objList}" var="li">
                    <apex:column value="{!li.Name}"/>
                    <apex:column value="{!li.Discount__c}"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
            <apex:pageBlockButtons>
                <apex:commandButton value="Get Profiles" action="{!settingsClass}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>```
1
  • This is an additional question and it is not appropriate to post as an answer. could you please post a new question for this, as this is different from your initial question
    – Shamina
    Jan 28, 2021 at 11:02

You must log in to answer this question.