How can we assign Custom permissions to profiles through apex in Salesforce?

How can we assign Custom permissions to profiles through apex in Salesforce?

Sample Code:
Assigning it to Permission Set:
SetupEntityAccess objSEA = new SetupEntityAccess();
objSEA.SetupEntityId = ‘0CP4x000000Pd5y’;//Id of the Custom Permission
objSEA.ParentId = ‘0PS4x000002YEdd’;//Id of the Permission Set
insert objSEA;

Assigning it to Profile:
SetupEntityAccess objSEA = new SetupEntityAccess();
objSEA.SetupEntityId = ‘0CP4x000000Pd5y’;//Id of the Custom Permission
objSEA.ParentId = [ SELECT Id, Name, Profile.Name FROM PermissionSet WHERE Profile.Name = ‘System Administrator’ ][ 0 ].Id;
insert objSEA;

Leave a Reply