1

We created a tool that scans Permission Sets of a installed managed package, "clones" these Permission Sets and removes any Permissions defined as too powerful.

However upon FieldPermissions Insert i get the following Error:

System.DmlException Insert failed. First exception on row 2803; first error: DUPLICATE_VALUE, duplicate value found: < unknown> duplicates value on record with id: < unknown>: [], 2018-07-18 11:56:41

consider the following piece of code:

// Used to create a new set or get an existing one
PermissionSet clone = fetchCloneSet();  
if(clone.Id == null) {
    insert clone;
}

// Get New Permissions to be granted
List<ObjectPermissions> objectPermissions = fetchObjectPerms();
List<FieldPermissions> fieldPermissions = fetchFieldPerms();
List<SetupEntityAccess> setupEntityAccessItems = fetchSetupEntityPerms();

// Delete existing Permissions
delete [SELECT Id FROM SetupEntityAccess WHERE ParentId = :clone.Id];
delete [SELECT Id FROM FieldPermissions WHERE ParentId = :clone.Id];
delete [SELECT Id FROM ObjectPermissions WHERE ParentId = :clone.Id];

// Insert new Permissions
insert objectPermissions;
insert fieldPermissions;
insert setupEntityAccessItems;

The Error occurs upon FieldPermissions insert. I know that because the Permission Set causing the error should get less than 1000 Object- and SetupEntity Permissions but over 2800 FieldPermissions.

After some Investigation I found some articles about the topic in this StackExchange Question. Seems Like there are DML errors and Deployment Errors with this message. However the DML Errors seem to be connected to a Custom Field set to "Unique" and FieldPermissions does not have a unique field according to the schema.

Any ideas what this very unhelpful error message is about?

  • I'm not certain, but I'd be surprised if there wasn't a uniqueness rule for FieldPermissions object on ParentId, SObjectType, and Field fields. Is there something in fetchFieldPerms that's creating a duplicate of those three fields? – pchittum Jul 19 '18 at 8:01
  • @pchittum No, essentially I am executing the same query as in the delete FieldPermissions Line (line 14 in my example) and remove certain FieldPermissions from the list. So basically I query the list, remove some permissions, delete the original list and insert my reduced one after blanking the Ids. Regarding Unique fields, yes it makes sense that those fields are protected from duplication in some way, but the DUPLICATE_VALUE error has so far only come up if fields are marked as "Unique" in the Schema. – Robert Méndez Jul 19 '18 at 8:14
1

After some more research I found the solution:

The FieldPermission that caused the error (index 2803) was a custom Field on the Standard Event Object. Event and Tasks are somewhat connected by being a kind of Activity. If we check the FieldPermissions Documentation, we see the following part about FieldPermissions on Activity Objects:

While tasks and events are considered separate objects, they share a common set of activity custom fields. As a result, when a custom task field is created, a custom event field is also created, and vice versa. You can display the custom field on the event layout, task layout, or both event and task layouts.

Although custom activity fields are shared between tasks and events, you’ll see separate FieldPermissions records for the task and event. However, changes made to one field permission record are automatically made to the other. For example, if you create a custom activity field, assign field permissions to it in a permission set, and run the following query, the query will return two records with the same permission value.

So FieldPermissions for Event and Task are essentially the same thing in two records. By inserting these two FieldPermissions at the same time the platform threw the Duplicate error.

I adjusted my code (when inserting FieldPermissions for a Field on Event or Task) to only insert one of the two and ignore the other. Now its working without problems.

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.