I'm trying to set up a trigger in Salesforce to automatically delete a contact in Marketing Cloud (MC) whenever a contact is deleted in Salesforce (SF).
What I've Done:
- Followed the instructions on https://help.salesforce.com/s/articleView?id=sf.mc_cab_best_practices.htm&language=en_US&type=5 to create an API endpoint in MC for deleting contacts.
- Created a new trigger on the Contact object in SF to fire on after delete:
- Selected "Contact" object
- Accessed the "Trigger" menu and clicked "New"
- Pasted the trigger code (attach screenshot of code in comments)
- Enabled debug logs for the trigger
The Problem:
When I create and then delete a contact, I don't see any information related to the trigger in the Salesforce debug logs. The mock API endpoint in MC also doesn't show any calls being made.
Questions:
- Are there any errors in my approach?
- Could there be issues with the trigger code itself?
- How can I troubleshoot the trigger and ensure it's calling the MC API on contact deletion?
I'd appreciate any insights on how to fix this and successfully trigger the MC API call from Salesforce on contact deletion.
Here are code :
trigger ContactDelete on Contact (after delete) {
Set<Id> contactIds = Trigger.oldMap.keySet();
String url = 'https://demo1955825.mockable.io/contacts/v1/contacts/actions/delete'; // Replace with your actual URL
String token = 'YOUR_ACCESS_TOKEN_HERE'; // Replace with your access token
// Prepare the request body
Map<String, Object> requestBody = new Map<String, Object>();
requestBody.put('values', contactIds);
requestBody.put('deleteOperationType', 'ContactAndAttributes');
for (Id contactId : contactIds) {
// Call the URL with the contactIds in the body
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(url);
req.setMethod('POST');
req.setHeader('Authorization', 'Bearer ' + token);
req.setBody(JSON.serialize(requestBody));
HttpResponse res = h.send(req);
// Handle the response (optional)
if (res.getStatusCode() != 200) {
System.debug('Error calling URL: ' + res.getStatus() + ' - ' + res.getBody());
}
}
}