0

I want to retrieve name of Profile for which a hierarchy type of custom setting is defined. This custom setting is not organisation wide default, but is specific for a profile. I got to know recently in this question that 'Location' field of custom setting is a polymorphic field and can't be accessed directly, but needs TYPEOF clause.

I have used following query to access the profile name but it is returning me a code(prefix ID probably) of that profile rather than name:

SELECT setupOwner.Name, customField1__c, customfield2__c 
    FROM myUserSetting__c WHERE 
    SetupOwnerId NOT IN (SELECT Id FROM Organization)

Then I tried using following query:

SELECT TYPEOF What
    WHEN myUserSetting__c THEN setupOwner.Name,
    ELSE customField1__c, customfield2__c 
    FROM myUserSetting__c 
    WHERE SetupOwnerId NOT IN (SELECT Id FROM Organization)

But it is giving me syntax error. Not able to understand the typeof syntax and how to use it.

1

In my custom setting, I have following:

enter image description here

And to retrieve only profile names from custom setting, you may use as following code having a polymorphic query:

for(OverrideStandardButtons__c setting: [SELECT 
        TYPEOF SetupOwner
            WHEN Profile THEN Id, Name, UserType
        END
    FROM OverrideStandardButtons__c WHERE 
    SetupOwnerId IN (SELECT Id FROM Profile)]) {
    // fetch the profile defination
    Profile objProfile = setting.SetupOwner;

    System.debug(objProfile + '----' + objProfile.Name);
}

Result of code:

Profile:{Id=00e6F000002QmfaQAC, Name=System Administrator, UserType=Standard}----System Administrator

Profile:{Id=00e6F000002QmfcQAC, Name=Analytics Cloud Integration User, UserType=Standard}----Analytics Cloud Integration User

|improve this answer|||||
  • Thanks Rahul for the answer,. Additionally, to get the User name and other fields of custom setting(custom fields) I would sum the query to : SELECT TYPEOF SetupOwner WHEN Profile THEN Id, Name, UserType WHEN User THEN Name //gives username END, customField1__c, customField2__c, customField3__c FROM OverrideStandardButtons__c WHERE SetupOwnerId IN (SELECT Id FROM Profile) – Sarang Feb 14 '17 at 11:32
  • You are welcome, glad to be of any help! – Raul Feb 14 '17 at 12:15
0

In a polymorphic relationship, the referenced object of the relationship can be one of several objects.

The What relationship field of an Event could be an Account, a Campaign, or an Opportunity. When making queries or updating records with polymorphic relationships, you need to check the actual object type set for the relationship, and act accordingly.

For e.g.

SELECT 
  TYPEOF What
    WHEN Account THEN Name, Phone
    WHEN Opportunity THEN Amount, CloseDate
    ELSE Name, Email
  END
FROM Event

For more detail refer Polymorphic Keys and Relationships

|improve this answer|||||
  • Giving me error still: No such relation 'What' on entity 'myUserSetting__C '. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. Does it not work for custom settings? – Sarang Feb 14 '17 at 7:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.