Search code examples
permissionssalesforceapex-codeapex

How to check permission on custom setting from apex code


In Apex, given a custom setting "ThisSetting" containing items "ThisSettingFirst", "ThisSettingSecond", "ThisSettingThird", how would I correctly code these if statements?

if (the-currently-logged-on-user-doesnt-have-permission-to-read-custom-settings)

if (the-currently-logged-on-user-doesnt-have-permission-to-update-custom-settings)

if (the-currently-logged-on-user-doesnt-have-permission-to-read-custom-setting-ThisSetting)

if (the-currently-logged-on-user-doesnt-have-permission-to-update-custom-setting-ThisSetting)

if (the-currently-logged-on-user-doesnt-have-permission-to-read-custom-setting-ThisSettingFirst)

if (the-currently-logged-on-user-doesnt-have-permission-to-update-custom-setting-ThisSettingFirst)

Thanks.


Solution

  • You can use DescribeSObjectResult Class methods to check permissions of current user for that particular object/setting.

    Schema.sObjectType objType = Schema.getGlobalDescribe().get('ThisSetting__c');
    Schema.DescribeSObjectResult objDesc = objType.getDescribe();
    
    if(objDesc.isUpdateable()) {
        System.debug('Updateable');
    }
    if(objDesc.isCreateable()) {
        System.debug('Createable');
    }