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
LIMIT 1
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')) {
continue;
}
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.