How to execute Apex code in controller only for specific profile, like System Administrator? So that when other users visit Visualforce page, the code block would not be skipped?
Use This class let you check profile and based on this execute it or not. Try:
SYS_ADMIN_PROFILE_ID is variable that contains Id of System Administrator profile, you can get it by SOQL and keep in one of class as static |
|||||||||||||||||||||
|
You can query the user object to find out which profile the current user has:
Then you can either specify the id in a Custom setting, or hard-coded, if the case allows that. |
|||
|
You could use the code below. You would need to create a Set called setOfAllowedProfileNames that contains all the ProfileNames that you'd like the code to run for. The Set could be built from a CSV custom setting, or Custom Label. The benefit of this approach is that you are specifying the profile names rather than than the profile Ids, which will vary in different orgs.
|
||||
|
I believe you can also opt for hierarchy custom setting option for your scenario as it will provide the ability to perform different action based on logged in user/profile on apex classes. For more details ,follow the below link Regards Anchal Garg |
|||
|