This is a workaround. Salesforce only sends outbound messages on create and/or update of an object. Deletion is not available.
What happens in Salesforce:
- Once an object (for example
Account
) you're tracking gets removed a trigger is triggered. - If it's a
isDelete
trigger a custom object calledObjectToBeRemoved
gets created on the removal. It stores the class name and the ID of the object that is being removed. - An outbound message is being sent with the newly created
ObjectToBeRemoved
object.
What happens in the OutboundMessageBundle:
- Using the class name and the ID stored in the newly processed
ObjectToBeRemoved
, the bundle finds and removes the described object. - Finishing up,
ObjectToBeRemoved
is removed from Salesforce.
Add the ObjectToBeRemoved
document configuration to your config.yml
.
comsave_salesforce_outbound_message:
document_paths:
# ... your other document
ObjectToBeRemoved__c:
path: 'Comsave\SalesforceOutboundMessageBundle\Document\ObjectToBeRemoved'
- create the custom
ObjectToBeRemoved
object.
text 18 ObjectId__c
text 100 ObjectClass__c
- create an outbound message for the
ObjectToBeRemoved
object. The.wsdl
file for this object is included in theOutboundMessageBundle
and will be loaded automatically. - create class
ObjectsToRemoveScheduler
public without sharing class ObjectsToRemoveScheduler {
public static void scheduleForRemoval(List<SObject> objectItems) {
List<ObjectToBeRemoved__c> objectsToBeRemoved = new List<ObjectToBeRemoved__c>();
for (SObject objectItem: objectItems) {
ObjectToBeRemoved__c objectToBeRemoved = new ObjectToBeRemoved__c(
ObjectId__c = objectItem.Id,
ObjectClass__c = String.valueOf(objectItem).substring(0, String.valueOf(objectItem).indexOf(':'))
);
objectsToBeRemoved.add(objectToBeRemoved);
}
insert objectsToBeRemoved;
}
}
- create a trigger for every object you want to track deletion of
trigger AccountTrigger on Account (after delete, after insert, after undelete, after update, before delete, before insert, before update) {
if (Trigger.isBefore && Trigger.isDelete) {
ObjectsToRemoveScheduler.scheduleForRemoval(Trigger.old);
}
}
- BONUS: It would be wise to add a savepoint for the database. In case an exception happens and the object actually never gets removed in Salesforce. This will allow us to rollback.
// At the start:
Savepoint sp = Database.setSavepoint();
//In the catch:
Database.rollback(sp);