function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
satya damisetti 10satya damisetti 10 

Grant access for fields

Hi ,
Anyone can help me on this.
How to grant access for fields of an object of a particular profile  using apex?
Khan AnasKhan Anas (Salesforce Developers) 
Hi Satya,

Greetings to you!

Please refer to the below links which might help you further with the above requirement.

https://developer.salesforce.com/forums/?id=9060G0000005hnvQAA

https://salesforce.stackexchange.com/questions/159062/field-level-security-using-metadata-api

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Ajay K DubediAjay K Dubedi
Hi Satya,

You can use Permission Sets to assign permissions to certain users instead of having to clone Profiles.
See Admin Setup->Manage Users->Permission Sets.  

The Standard Salesforce UI makes adding/removing Users to/from Permission Sets challenging.
There is a free app on the AppExchange called The Permissioner which does a great job of making Permission
Sets usable for larger orgs.

You should follow this link for more help:
https://developer.salesforce.com/docs/atlas.en-us.securityImplGuide.meta/securityImplGuide/users_profiles_fls.htm
https://success.salesforce.com/answers?id=90630000000hJdsAAE
https://gethelp.drift.com/hc/en-us/articles/360019665113-How-to-Grant-the-Proper-Salesforce-User-and-Object-Permissions-for-Drift-and-Salesforce

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi
Deepali KulshresthaDeepali Kulshrestha
Hi Satya,
Greetings to you!

Please try this.

For a Specific Profile:

    List<FieldPermissions> fpList = [SELECT SobjectType, Field, PermissionsRead, PermissionsEdit, Parent.ProfileId FROM FieldPermissions WHERE SobjectType = 'Account' and Field='Account.Customer_Priority__c' AND Parent.ProfileId IN (SELECT Id FROM PermissionSet WHERE PermissionSet.Profile.Name = 'System Administrator')];
    if(!fpList.isEmpty()){
        Boolean hasReadPermission = fpList[0].PermissionsRead;
        Boolean hasEditPermission = fpList[0].PermissionsEdit;
        system.debug('Read Permission - ' + hasReadPermission);
        system.debug('Edit Permission - ' + hasEditPermission);
    }
    
For Current User:
    List<FieldPermissions> fpList = [SELECT SobjectType, Field, PermissionsRead, PermissionsEdit, Parent.ProfileId FROM FieldPermissions WHERE SobjectType = 'Account' and Field='Account.Customer_Priority__c' AND Parent.ProfileId=:Userinfo.getProfileId()];
    if(!fpList.isEmpty()){
        Boolean hasReadPermission = fpList[0].PermissionsRead;
        Boolean hasEditPermission = fpList[0].PermissionsEdit;
        system.debug('Read Permission - ' + hasReadPermission);
        system.debug('Edit Permission - ' + hasEditPermission);
    }

Reference Link : - 
    https://developer.salesforce.com/forums/?id=9060G0000005hnvQAA


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.