public void add(SObject record) {
records.add(record);
}
public void addAll(List records) {
this.records.addAll(records);
}
public void doWork() {
Database.insert(records, AccessLevel.SYSTEM_MODE);
}
}
public with sharing class ReservationScheduleServiceImpl implements IReservationScheduleService {
code
}
In case the assignment already exists for the correlation id, the previous assignment and schedule are moved to the recycle bin instead of being totally removed.
What changes are needed to remove them at once?
Assistant
To ensure that the previous assignment and its associated schedule are permanently removed (i.e., not just moved to the Recycle Bin), you need to make two key changes:
Retrieve the pse__Schedule__c records associated with the pse__Assignment__c records being deleted.
Add these pse__Schedule__c records to the deletion list.
Explicitly empty the Recycle Bin for the deleted records immediately after the Database.delete operation.
Here's a breakdown of the required changes:
1. Update AssignmentsSelector (Conceptual)
Assuming AssignmentsSelector is an fflib selector, you'll need to modify its query in selectByCorrelationIdsAsSystem to also retrieve the pse__Schedule__c Id from the pse__Assignment__c record.
apex
// In AssignmentsSelector.cls (or wherever selectByCorrelationIdsAsSystem resides)
public with sharing class AssignmentsSelector extends fflib_SObjectSelector {
// ... other methods ...
public List<pse__Assignment__c> selectByCorrelationIdsAsSystem(Set<String> correlationIds) {
// IMPORTANT: Ensure the query includes pse__Schedule__c
return Database.query(
'SELECT Id, pse__Schedule__c FROM pse__Assignment__c WHERE NT_Correlation_ID__c IN :correlationIds'
, AccessLevel.SYSTEM_MODE // Add SYSTEM_MODE if not already done by selector
);
}
}
// In ReservationServiceImpl.cls
public with sharing class ReservationServiceImpl implements IReservationService {
// ... existing code ...
private static void deleteAssignments(List<Reservation> reservations, fflib_ISObjectUnitOfWork uow) {
DeleteSObjectsWork work = new DeleteSObjectsWork();
uow.registerWork(work);
Set<String> correlationIds = getCorrelationIds(reservations);
// 1. Select Assignments and their associated Schedule IDs
List<pse__Assignment__c> assignmentsToDelete = AssignmentsSelector.newInstance().selectByCorrelationIdsAsSystem(correlationIds);
Set<Id> scheduleIdsToDelete = new Set<Id>();
for (pse__Assignment__c assignment : assignmentsToDelete) {
if (assignment.pse__Schedule__c != null) {
scheduleIdsToDelete.add(assignment.pse__Schedule__c);
}
}
// 2. Query for the Schedule records themselves
List<pse__Schedule__c> schedulesToDelete = new List<pse__Schedule__c>();
if (!scheduleIdsToDelete.isEmpty()) {
schedulesToDelete = [SELECT Id FROM pse__Schedule__c WHERE Id IN :scheduleIdsToDelete WITH SECURITY_ENFORCED]; // Use WITH SECURITY_ENFORCED unless SYSTEM_MODE is truly intended here
}
// 3. Add both lists to the work unit
work.addAll(assignmentsToDelete);
work.addAll(schedulesToDelete);
}
// ... existing code ...
}
3. Modify DeleteSObjectsWork.doWork()
This custom work action is the perfect place to add the Database.emptyRecycleBin() call, ensuring that any records just deleted are also permanently removed.
apex
// In DeleteSObjectsWork.cls
public with sharing class DeleteSObjectsWork implements fflib_SObjectUnitOfWork.IDoWork {
private final List<SObject> records = new List<SObject>();
public void add(SObject record) {
records.add(record);
}
public void addAll(List<SObject> records) {
this.records.addAll(records);
}
public void doWork() {
if (!records.isEmpty()) {
// First, delete the records (they will go to the Recycle Bin)
Database.delete(records, AccessLevel.SYSTEM_MODE);
// Then, immediately empty the Recycle Bin for these specific records
// This permanently removes them from the system.
Database.emptyRecycleBin(records, AccessLevel.SYSTEM_MODE);
}
}
}
Summary of Changes and Rationale:
AssignmentsSelector modification: Ensures that when you query for pse__Assignment__c, you also retrieve the ID of its associated pse__Schedule__c. This is crucial for identifying which schedules need to be deleted.
Collects the Ids of all pse__Schedule__c records linked to the assignments found.
Performs a second query to fetch the actual pse__Schedule__c SObjects based on these IDs.
Adds both the pse__Assignment__c and pse__Schedule__c lists to the DeleteSObjectsWork instance. This ensures both related objects are targeted for deletion.
DeleteSObjectsWork.doWork() modification:
The standard Database.delete() method moves records to the Recycle Bin by default.
Database.emptyRecycleBin(List<SObject> records, AccessLevel) allows you to permanently remove specific records from the Recycle Bin. By calling it immediately after the Database.delete, you achieve the "totally removed" effect in a single logical transaction block.
Adding an if (!records.isEmpty()) check prevents unnecessary DML calls.
These changes will ensure that when an existing assignment (identified by NT_Correlation_ID__c) is upserted, its previous version and its corresponding schedule are permanently removed from Salesforce.