调用Schema.sObjectType.UserLicense.isAccessible()时遭遇“Entity is not api accessible”错误的解决方法求助
Hey there, let's work through that error you're hitting when using Schema.sObjectType.UserLicense.isAccessible(). I've dealt with similar setup object permission issues before, so here are actionable steps to resolve this:
1. Verify Object and Administrative Permissions in Profile/Permission Set
The UserLicense object is a setup entity, so it requires two key permissions:
- Object-level Read Access: Go to Setup → Profiles (or Permission Sets) → [Your Profile/Permission Set] → Object Settings → User License. Ensure the "Read" permission is enabled.
- View Setup and Configuration: In the same Profile/Permission Set, navigate to Administrative Permissions and make sure "View Setup and Configuration" is checked. This permission is required to access most setup-related objects via the API.
2. Check the Execution Context of Your Code
- If your Apex code runs in the context of a non-admin user, even if you use the
without sharingkeyword, the underlying user still needs the object-level permissions mentioned above. Thewithout sharingmodifier bypasses sharing rules but doesn't grant object access. - For scheduled jobs or batch Apex, confirm the running user has the necessary permissions. You might need to assign the required permission set to the running user.
3. Ensure API Enabled Permission is Active
If your code is being called via an API (REST/SOAP) or a connected app:
- Go to the user's Profile/Permission Set → Administrative Permissions → Enable "API Enabled".
- For connected apps, verify that the app has been granted access to the
UserLicenseobject (check this in Setup → Apps → Connected Apps → Manage Connected Apps → [Your App] → OAuth Settings → Selected OAuth Scopes to ensure appropriate access).
4. Test with Direct SOQL Query as an Alternative
Sometimes the isAccessible() check can behave unexpectedly for setup objects. Try running a simple SOQL query to confirm access:
List<UserLicense> licenses = [SELECT Id, Name FROM UserLicense LIMIT 5]; System.debug('User Licenses: ' + licenses);
If this query returns results, it means the user has access, and the issue might be with how the schema check is being handled. In that case, you could use the SOQL query as a fallback to verify access instead of relying solely on isAccessible().
内容的提问来源于stack exchange,提问作者Ramya

