I have created a stateful Apex Batch Job which is processing Permission Sets defined by a Set of Ids. I want to process each Permission Set individually so the batch size is 1.
The batch job creates the correct number of batches and completes successfully, however not all Permission Sets retrieved in the "start" method get processed.
By debugging and logging processed Ids I can see that a certain number of Permission Sets actually never enter the "execute" method. Instead some Permission Set records get processed twice.
here is my code:
public class AnalyzePermissionSets implements Database.Batchable<SObject>, Database.Stateful {
private Set<Id> setIds;
//set to debug processed Permission Sets
private Set<Id> processedSets;
// CONSTRUCTOR
public AnalyzePermissionSets(Set<Id> incomingIds) {
this.setIds = incomingIds;
this.processedSets = new Set<Id>();
}
// BATCHABLE METHODS
public Database.QueryLocator start(Database.BatchableContext BC) {
return Database.getQueryLocator([SELECT Id, Name, NameSpacePrefix, (SELECT Id FROM ObjectPerms)
FROM PermissionSet
WHERE Id IN :setIds]);
}
public void execute(Database.BatchableContext BC, List<PermissionSet> scope) {
PermissionSet pSet = scope.get(0);
// Within this method i am manipulating the ObjectPermissions,
//FieldPermissions and SetupEntityAccessItems of the Permission Set
process(pSet);
// logging Id to see which Sets actually have been processed
processedSets.add(pSet.Id);
}
public void finish(Database.BatchableContext BC) {
System.debug(setIds.size());
System.debug(processedSets.size());
}
}
An example:
- created 1 Batch job with 8 Batches total
- batch job completes sucessfully
- setIds.size() = 8 and processedSets.size() = 4
I found a kind of similar question here: Batch apex failling silently for some records
But I tested this in a Sandbox and Developer Edition and both times almost the exact same number of Permission Sets did not get processed (difference was one set). The Permission Sets are mostly part of a external managed Package but also custom.
Any ideas how to solve this? Let me know if you need further information.