I have a class where I check if the User has ModifyAllData permission:

SELECT PermissionSet.PermissionsModifyAllData
FROM PermissionSetAssignment
WHERE AssigneeId = :UserInfo.getUserId()
      AND PermissionSet.PermissionsModifyAllData = TRUE

In my unit test I want to create PermissionSet with PermissionsModifyAllData = true but I have the following kind of errors when I deploy the code:

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Permission ViewAllData depends on permission(s): ViewAllForecasts

which varies from sandbox to sandbox (or scratch), depending on which features are enabled in the org.

How I can get all the dependent permission for ViewAllData (ModifyAllData)?

One way I found is by using DescribeSObjectResult and enabling all Permissions, but is there any better solution?

PermissionSet modifyAllData = new PermissionSet(
    Label = 'Test_Modify_All_Permission_Set',
    Name = 'Test_Modify_All_Permission_Set',
    PermissionsModifyAllData = true,
    PermissionsViewAllData = true

DescribeSobjectResult permissionSetDescribe = Schema.PermissionSet.SObjectType.getDescribe();
Map<String, SObjectField> fieldMap = permissionSetDescribe.fields.getMap();

for (String fieldName : fieldMap.keySet()) {
    if (!fieldName.startsWithIgnoreCase('Permissions')) {
    DescribeFieldResult fieldDescribe = fieldMap.get(fieldName).getDescribe();
    if (fieldDescribe.isCreateable() && fieldDescribe.isUpdateable()) {
        modifyAllData.put(fieldDescribe.getName(), true);

insert modifyAllData;

The above solution gives me the following errors:

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, The user license doesn't allow the permission: OptOutGeoLocationTracking: []

which means that not all available permissions could be enables because of the user's licence.

  • why not find an active user that is profile = System Administrator as that profile as Modify All Data configured by default and can't be changed?
    – cropredy
    Nov 15, 2019 at 19:39

1 Answer 1


From this help article:

  • Read, Create, Edit, Delete, View All, Modify All on all standard and custom objects
  • Convert Leads
  • Create and Customize Reports
  • Edit Events
  • Edit Personal Quota
  • Edit Tasks
  • Import Leads
  • Import Solutions
  • Manage Categories
  • Manage Dashboards
  • Manage Public Documents
  • Manage Public List Views
  • Manage Public Reports
  • Manage Public Templates
  • Override Forecasts
  • Run Reports
  • Transfer Leads
  • Transfer Record
  • Use Team Reassignment Wizards
  • View All Data
  • View All Forecasts
  • View Setup and Configuration
  • If I list all those permissions, then I get errors like Field does not exist: PermissionsViewDataLeakageEvents on PermissionSet depending on the features enabled in the org. I need somehow dynamically get all dependent permissions that available in the org.
    – edelrabe
    Nov 15, 2019 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.