You need to sign in to do that
Don't have an account?
Dosbol 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.
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()); } } }
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