0

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:

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:

  1. Are there any errors in my approach?
  2. Could there be issues with the trigger code itself?
  3. 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());
    }
  }
}

Trigger code Debug log Empty log when delete contact

1

1 Answer 1

0

@Teacher Lemon An Apex trigger can execute a callout when the callout is invoked within a method defined as asynchronous: that is, defined with the @future keyword.

Please check this link : how to make callouts from apex trigger

2
  • Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
    – identigral
    Commented May 22 at 4:28
  • It's strange that I tried changing the code, only calling system.debug to get the contactIds that have been deleted, but the debug log is still empty. I also tried creating an apex class and calling in the apex trigger but to no avail. How can I view the debug log to know what's going on, why the trigger doesn't hit? Commented May 23 at 15:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .