function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jonny.KatesJonny.Kates 

Using Apex to hit an external URL on Contact delete

Hey guys, I'm a self-confessed Apex amateur and was wondering if I could get a hand with something.

 

I'm trying to get Salesforce to hit an external URL on Contact delete. Our site has a registration process which feeds SalesForce new Contacts. From that point on, the two entities in seperate databases (i.e. website.user and salesforce.contact) are synchronised. However, I want to make it so that when a Contact is deleted in SalesForce, their c.ID is pushed to the website by method of a URL suffix. This means that the website can simply 'listen' to SalesForce rather than scan the database for any missing Contact records.

 

This is probably horrendous - and low and behold doesn't work - but can someone give me a hand with this Apex class?:

 

public class ContactDeleteURL {
public static void hitTheServer(Contact[] con) {
for (Contact c:con){
HttpRequest req = new HttpRequest();
req.setEndpoint ('http://www.mywebsite.com/remove_contact?sfid=' + c.Id);
req.setMethod ('POST');
}
}
}

 ..and trigger:

trigger ContactRecordDelete on Contact (after delete) {
Contact[] con = Trigger.new;
ContactDeleteURL.hitTheServer(con);
}

 I'm getting the following error when I try to delete a Contact, if that helps:

 

ContactRecordDelete: execution of BeforeDelete

caused by: System.NullPointerException: Attempt to de-reference a null object

Class.ContactDeleteURL.hitTheServer: line 3, column 16
Trigger.ContactRecordDelete: line 3, column 1

 

Shashikant SharmaShashikant Sharma

Just change it to

 

trigger ContactRecordDelete on Contact (after delete) {
Contact[] con = Trigger.old;
ContactDeleteURL.hitTheServer(con);
}

 Trigger.new is not available in after detele trigger. It should solve the attempt to dereference issue.

Ravi kumar 292Ravi kumar 292
Hi Shashikanth,

I have a same scenario.. Whenever a lead is created on Salesforce we need to hit the client URL with LeadId and ContactId. Done the same as above but am unable to hit the client URL.

Please help on this.

Code : 
Public class testURL{
    public static void hitURL(Lead[] l){
    for (Lead lead:l){
    HttpRequest req = new HttpRequest();
    req.setEndpoint ('<a target="_blank" href="http://test.com/Internal/a/callback_acc.ashx?lead_id=ld121212&contact_id=234234">http://test.com/Internal/a/callback_acc.ashx?lead_id=ld121212&contact_id=234234</a>');
    req.setMethod ('POST');
    }
  }
}

Trigger:

trigger IndiaLendsURL on Lead (after insert) {
    Lead[] l = Trigger.new;
    testURL.hitURL(l);
}