5

I had the need to retrieve profile ids but i had reached SOQL limit , is there a way to retrieve profile ids without increasing the SOQL count?

9

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();

  • Thanks rohit , i 'am not sure but after some search found that we could use platform cache but wasn't able to formulate a working solution. – Rohit C Mar 7 '17 at 5:52
  • @RohitC I've updated my answer. – Rohit Mourya Mar 7 '17 at 6:04
  • ok , i tried your approach and it helped in reducing the SOQL count. Thanks. – Rohit C Mar 7 '17 at 6:20
  • Nice! I didn't know about platform cache. – Mahmood Mar 7 '17 at 17:03
2

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.

  • Thanks Rajesh , but i wanted to use it in a test class. So didn't want to write a batch for just a test class. – Rohit C Mar 7 '17 at 6:23
2

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

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.