10

How to determine in APEX whether specific profile is custom or not?

There is a checkbox on a profile page that says "Custom Profile", but I couldn't find any field on the Profile object to be selected by a query. Is there a way to determine custom profiles using only APEX and SOQL?

9

Unfortunately there is no 'IsCustom' field or anything similar on the Profile object.

The only workaround that I know of is to compare the CreatedDate of your Profiles to the CreatedDate of you org.

Organization org  = [SELECT Id , CreatedDate FROM Organization]; 
List<Profile>  customProfiles = 
[
    SELECT Id, Name, CreatedDate 
    FROM Profile 
    WHERE CreatedDate > :org.CreatedDate
];

This is not a perfect solution as in one of my orgs this still bring back the 'Chatter Free User', 'Chatter Moderator User', 'Chatter External User' and 'Guest License User'. You can filter these values out, although I'm not sure if the below will work in non-English language orgs.

Organization org  = [SELECT Id , CreatedDate FROM Organization]; 
List<Profile>  customProfiles = 
[
    SELECT Id, Name, CreatedDate 
    FROM Profile 
    WHERE CreatedDate > :org.CreatedDate 
    AND Name NOT IN('Chatter Free User', 'Chatter Moderator User', 'Chatter External User', 'Guest License User')
];
9

Since this question was posted, a new field was added as of the Spring release API Version 39 - IsCustom - to the permissionSet object. Since the permissionset object is an amalgamation of the Profile and PermissionSets, you can simply use a query to accommodate this use case.

List<PermissionSet> profiles = new List<PermissionSet>([SELECT Id, Profile.Name
                                                        FROM PermissionSet
                                                        WHERE IsOwnedByProfile = true AND IsCustom = true]);

Please note that in the SOQL Query the permissionSet object has a field called "IsOwnedByProfile" to denote that the field is a permissionSet or a Profile and IsCustom denotes whether or not the profile is standard or custom.

Please also note that at the time of writing this post there is not a field called IsCustom on the profile object, except with the usage of the metadata API - denoted by the tag.

Spring '17 Release Notes Changed Object List (includes information about new IsCustom field)

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.