How do you / can you assign a Record Type to a Permission Set in Apex code?

I have a post-install handler that is executed as part managed package installation. One of the tasks performed by the post-install handler is to create a Permission Set, and assign some object permissions to this Permission Set. This works all well and good; You create an instance of a PermissionSet SObject and insert it, and then create instances of ObjectPermission objects, specifying the SObject Type and the CRUD permissions you wish to grant and then set the ParentId of the ObjectPermission objects to the Id of the PermissionSet you created earlier. No problem. But now I have a new requirement. I need to grant access to certain Record Types within this Permission Set. For example, there’s an Account Record Type that is included as part of our managed package. I want to make this Record Type “visible” in the Permission Set as part of the post-install task. I cannot figure out the mechanism for doing this via Apex code. In fact, I’m not sure it is supported. There doesn’t seem to be an SObject equivalent to the ObjectPermissions object for Record Type permissions. There IS something called a SetupEntityAccess object, but this appears to be used for granting access to Apex classes and/or Visualforce pages. Anyone out there know how to do what I’m trying to do, or if it is even possible with straight Apex / DML?


You might need to use the Metadata API currently. PermissionSet exposes the recordTypeVisibilities collection of PermissionSetRecordTypeVisibility records.

Indicates which record types are visible to users assigned to this permission set. Available in API version 29.0 and later. This field is never retrieved or deployed for inactive record types.

There is also the existing idea: Record Type Assignment for Permission Sets via Apex

Source : Link , Question Author : tlfu , Answer Author : Daniel Ballinger

Leave a Comment