How to get Profile Ids without SOQL query?

I guess there is no way to retrieve all profiles without using SOQL/SOSL. However you can use Platform Cache to store data and later retrieve it. But still you will need SOQL to retrieve List of profiles and then put it in cache. Sample example would be:

List<Profile> profileList = [SELECT Id,Name FROM Profile LIMIT 100];
Cache.Org.put('light002.partition1.profileList', profileList);
if (Cache.Org.contains('light002.partition1.profileList')) {
    profileList = (List<Profile>)Cache.Org.get('light002.partition1.profileList');
}

Maybe if you could post your code, community would help you to optimize your code.

However, for accessing logged in User profile id you can use following methods:

For Visualforce page use: {!$Profile.Id}. Here is the $Profile link

And for Apex code:UserInfo.getProfileId();


You can also create a custom setting and store all the profile ids in the key of the custom setting. Since Profiles are not created/deactivated everyday, the custom setting would be easy to manage. You can also write a daily batch to keep the custom setting data in sync with the profile Ids.

Once you have the profile ids in custom setting, you can use CustomSetting_Name__c.getAll() to retrieve all the ids without consuming any SOQL.


Custom settings could be an option but it is not best practice to store the Profile Ids in custom settings as it would cause lots of problems. For example, if you have more than one environments, the profile Ids will be different.

Thus we can go back to the question how/why the limit is reached. There are multiple ways to avoid hitting limit such as bulktify the code, using @future etc

Tags:

Soql