6

I can't create a ViewAllData PermissionSet via Apex for a test class.

Here is my code:

PermissionSet ps = new PermissionSet(name = permSetName, label = permSetName);
insert ps;

List<ObjectPermissions> opList = new List<ObjectPermissions>();
opList.add(new ObjectPermissions(SObjectType = 'Account', PermissionsRead = true, PermissionsViewAllRecords = true));
opList.add(new ObjectPermissions(SObjectType = 'Asset', PermissionsRead = true, PermissionsViewAllRecords = true));
opList.add(new ObjectPermissions(SObjectType = 'Campaign', PermissionsRead = true, PermissionsViewAllRecords = true));
opList.add(new ObjectPermissions(SObjectType = 'Case', PermissionsRead = true, PermissionsViewAllRecords = true));
opList.add(new ObjectPermissions(SObjectType = 'Contact', PermissionsRead = true, PermissionsViewAllRecords = true));
opList.add(new ObjectPermissions(SObjectType = 'Contract', PermissionsRead = true, PermissionsViewAllRecords = true));
opList.add(new ObjectPermissions(SObjectType = 'Document', PermissionsRead = true, PermissionsViewAllRecords = true));
opList.add(new ObjectPermissions(SObjectType = 'Idea', PermissionsRead = true);
opList.add(new ObjectPermissions(SObjectType = 'Lead', PermissionsRead = true, PermissionsViewAllRecords = true));
opList.add(new ObjectPermissions(SObjectType = 'Opportunity', PermissionsRead = true, PermissionsViewAllRecords = true));
opList.add(new ObjectPermissions(SObjectType = 'Pricebook2', PermissionsRead = true, PermissionsViewAllRecords = false));
opList.add(new ObjectPermissions(SObjectType = 'Product2', PermissionsRead = true, PermissionsViewAllRecords = false));
opList.add(new ObjectPermissions(SObjectType = 'PushTopic', PermissionsRead = true, PermissionsViewAllRecords = false));
opList.add(new ObjectPermissions(SObjectType = 'Solution', PermissionsRead = true, PermissionsViewAllRecords = true));
opList.add(new ObjectPermissions(SObjectType = VaultUtils.getNamespace() + 'ArchivedChatterGroup__c', PermissionsRead = true, PermissionsViewAllRecords = true));
opList.add(new ObjectPermissions(SObjectType = VaultUtils.getNamespace() + 'ArchivedChatterMessage__c', PermissionsRead = true, PermissionsViewAllRecords = true));
opList.add(new ObjectPermissions(SObjectType = VaultUtils.getNamespace() + 'ArchivedFeed__c', PermissionsRead = true, PermissionsViewAllRecords = true));

for (ObjectPermissions op : opList) {
    op.ParentId = ps.Id;
}
insert opList;

ps.put('PermissionsViewAllData', true);
update ps;

The error is

System.DmlException: Insert failed. First exception on row 7; first error: FIELD_INTEGRITY_EXCEPTION, The following permission isn't allowed: Read All Idea: []

If I include the line referring to Idea, it gives that error, though I haven't specified a value for that field. If I specify that PermissionsViewAllRecords is false, it gives the SAME error. And if I comment just the Idea line, I get the same problem with Pricebook2, and so on.

If I comment the lines that give that error, it gives this error

System.DmlException: Update failed. First exception on row 0 with id 0PS#######; first error: FIELD_INTEGRITY_EXCEPTION, Permission View All Data depends on permission(s): Read Idea, Read Pricebook2, Read Product2, Read PushTopic: []

which is expected. But I can't include PermissionsRead - either with PermissionsViewAllRecords = false, nor without specifying a value for PermissionsViewAllRecords.

The same error happens for Pricebook2, Product2, and PushTopic.

It looks like it fails, no matter what I do. Has anyone had any luck making a PermissionSet via Apex with ViewAllData?

I need it because I can't package that permission in a managed package PermissionSet, but the permission is necessary. The customer will need to make a PermSet manually with that permission, but I have to recreate it in my unit tests.

(Side note: If I specify PermissionsViewAllData on the PermissionSet without the ObjectPermissions records, it gives the "depends on permission(s)" error as well.)

  • That is a stumper, I might suggest going ahead and logging a case with support. Hopefully someone has a workaround, but this may just be an oversight in how SFDC has setup permission sets, and getting that up to R&D to fix is a pretty long and slow process. – Ralph Callaway Jul 9 '14 at 21:43
2

I have reproduced the issue you are describing in Spring '14 and the root cause is identical to this previous question: Cannot create a Permission Set related to an Idea via Console or Anonymous. The good news: it should be fixed in Summer '14 (coming to an instance near you soon - see trust.salesforce.com for release schedule); bad news: no known workaround for unit tests in Apex.

Apologies for any inconvenience this issue has caused. Please report back if you have an issue once your instance has been upgraded to Summer '14.

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.