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
Dosbol TDosbol T 

Code to handle field permissions on Permission sets

Hi there, I am trying to write a code which would help me to grant read accesses to the fields on Permission sets. Please help me with this code below. It does not show any error but also it is not working.
Any advice, help will be highly appreciated.
public class GrantFieldAccess {

    public void grantFieldReadAccess() {
        List<String> fieldAPINames = new List<String>{
            'CustomeField1',
            'CustomeField2'
        };

        List<PermissionSet> permissionSets = [SELECT Id FROM PermissionSet WHERE Name IN ('PermissionSet1', 'PermissionSet2', 'PermissionSet3' )];

        List<FieldPermissions> fieldPermissionsList = new List<FieldPermissions>();

        for (PermissionSet ps : permissionSets) {
            for (String fieldAPIName : fieldAPINames) {
                FieldPermissions fieldPerm = new FieldPermissions(
                    SobjectType = 'Account',
                    Field = fieldAPIName,
                    ParentId = ps.Id,
                    PermissionsRead = true
                );
                fieldPermissionsList.add(fieldPerm);
            }
        }

        try {
            upsert fieldPermissionsList;
            System.debug('Field access granted successfully.');
        } catch (Exception ex) {
            System.debug('An error occurred: ' + ex.getMessage());
        }
    }
}

 
SwethaSwetha (Salesforce Developers) 
HI Dosbol,
The code you provided seems to be correct, but there could be a few reasons why it is not working as expected. 

>  Ensure that the field API names ('CustomeField1' and 'CustomeField2') are correct. Double-check the spelling and capitalization of the field names.
> Make sure that the permission set names ('PermissionSet1', 'PermissionSet2', 'PermissionSet3') are correct. Check for any typos or case sensitivity issues.
> Confirm that the object ('Account') and the fields you are trying to grant access to exist in the org. Also, ensure that the fields are visible to the user's profile or permission set.
> Enable debug logs to check for any error messages or exceptions that might be occurring during the execution of the code. This can provide more insights into the issue.
> Verify that the user executing the code has the necessary permissions to update field-level security settings. They should have the "Customize Application" permission enabled in their profile or permission set.
> Instead of using DML operations to update field permissions, you can also consider using the Salesforce Metadata API to update field-level security settings programmatically. 

If this information helps, please mark the answer as best. Thank you
Lexi KansLexi Kans
Handling field permissions on Salesforce Permission Sets involves controlling which fields users can view or edit in an organized manner. This can be achieved programmatically using Salesforce's Metadata API or Apex code. With Apex, you can create a method to update field permissions on a Permission Set. This method takes the name of the Permission (https://depthcrypto.com/)Set, the API name of the object, and a map of field API names with corresponding boolean values indicating edit permissions. The method then queries for the Permission Set, retrieves the object's fields, and constructs FieldPermissions objects based on the provided permissions. These FieldPermissions are then upserted to update the field permissions. This approach allows you to manage field access efficiently based on the permissions defined in the code.