The current implementation handles existing assignments by moving them to the recycle bin. To remove them at once, you need to modify the deleteAssignments method in ReservationServiceImpl and potentially the AssignmentsSelector class. Here's a breakdown of the changes needed:
1. Modify deleteAssignments in ReservationServiceImpl:
Instead of using AssignmentsSelector.newInstance().selectByCorrelationIdsAsSystem(correlationIds), you should directly query and delete the existing assignments. This will bypass the recycle bin mechanism.
private static void deleteAssignments(List<Reservation> reservations, fflib_ISObjectUnitOfWork uow) {
DeleteSObjectsWork work = new DeleteSObjectsWork();
uow.registerWork(work);
Set<String> correlationIds = getCorrelationIds(reservations);
List<pse__Assignment__c> assignments = [SELECT Id FROM pse__Assignment__c WHERE NT_Correlation_ID__c IN :correlationIds];
work.addAll(assignments);
uow.commitWork();
}
Explanation of the change:
- We directly query the
pse__Assignment__c object using a SOQL query to find all assignments with correlation IDs present in the correlationIds set.
- The query
SELECT Id FROM pse__Assignment__c WHERE NT_Correlation_ID__c IN :correlationIds efficiently retrieves the IDs of the assignments to be deleted.
- We then add these retrieved assignments to the
DeleteSObjectsWork for deletion.
- Finally, we commit the work to perform the deletion.
2. Consider the AssignmentsSelector Class (Optional but Recommended):
The AssignmentsSelector class likely handles the selection of assignments based on various criteria. You might want to update this class to directly support deleting assignments by correlation ID without the recycle bin behavior. However, since you're directly querying in ReservationServiceImpl, this might not be strictly necessary for this specific change.
Complete Updated Code Snippets:
ReservationServiceImpl.java:
private static void deleteAssignments(List<Reservation> reservations, fflib_ISObjectUnitOfWork uow) {
DeleteSObjectsWork work = new DeleteSObjectsWork();
uow.registerWork(work);
Set<String> correlationIds = getCorrelationIds(reservations);
List<pse__Assignment__c> assignments = [SELECT Id FROM pse__Assignment__c WHERE NT_Correlation_ID__c IN :correlationIds];
work.addAll(assignments);
uow.commitWork();
}