[SalesForce] Is it possible to query the Apex Class Accesses of a permission set

I'm not sure about the object name and the Field Name. I tried but i think "ApexClass" is not the right object to call apexclass from a permission set.
Here is the query
[Select Id, Name From ApexClass] but its not giving the enabled classes in a permissionSet.

Thanks in Advance

Best Answer

You can use SetupEntityAccess and Apex class to get required information.

First SOQL will show all apex class Ids which is enabled for permission set 'CustomPermission'.

Second SOQL will show Apex Class Name based on first query result.

Map<ID,AggregateResult> mapAccess=new Map<Id, AggregateResult>([SELECT SetupEntityId Id
FROM SetupEntityAccess where Parent.Name='CustomPermission' and SetupEntityType='ApexClass' GROUP By SetupEntityId]);

List<ApexClass> apexClasses=[Select Name from Apexclass where id in:mapAccess.keyset()];

System.debug(apexClasses);