1

I want to create salesforce web hook in php.Actually purpose is, if any record added in user object or contact object or account object, then there should come intimation in my application side where we are using SFDC api to sync user from salesforce to my application.

Can any one suggest to create webhook in SFDC using PHP ?

1
  • Outbound messages may fit the bill here
    – Eric
    Feb 4, 2017 at 7:15

1 Answer 1

1

If your intention is to get your PHP application notified every time a User, Account or Contact is added in Salesforce, you'll need to put PHP aside for a while and do a little Apex coding in order to setup things.

Your PHP application can receive a "ping" from Salesforce using an Apex HTTP Callout directed to your "listener" endpoint of choice (a PHP page you'll setup on a webserver somewhere). You can then pass along from Salesforce to your PHP code all the GET/POST parameter you in the Callout request (though exchanging data this way should be thought carefully security-wise).

This is an example Apex class you could use, taken from this article from Jeff Douglas and briefly re-adapted:

public class MyPHPWebHook {

@future (callout=true)
public static void sendNotification (String event, String id) {

    HttpRequest req = new HttpRequest();
    HttpResponse res = new HttpResponse();
    Http http = new Http();

    req.setEndpoint('http://my-end-point.com/web-hook-listener');
    req.setMethod('POST');
    req.setBody('event='+EncodingUtil.urlEncode(event, 'UTF-8')+'&id='+EncodingUtil.urlEncode(event, 'UTF-8'));

    try {
        res = http.send(req);
    } catch(System.CalloutException e) {
        System.debug('Callout error: '+ e);
        System.debug(res.toString());
    }

}

As per documentation remember to add your endpoint as allowed in SF Setup, under Remote Site Settings tab.

After creating the Class, you'll need to create (if doesn't exists one already) a trigger for each Salesforce object you want to "watch" - User, Account and Contact in your case.

Salesforce Documentation

Writing Your First Apex Class and Trigger

An example of a trigger sending a notification about an User having been created or updated in Salesforce, using the above class:

trigger UserTrigger on User (after insert, after update) { for (User userx: Trigger.new) { MyPHPWebHook.sendNotification(Trigger.isInsert ? 'user.insert' : 'user.update', userx.Id); } }

This solution is subject to fail if the records inserted/updated are more than 50 because only 50 @future method invocations per transaction are allowed.

In this solution I needed to use a @future method to perform my Callout because Callouts cannot be made after a Database event occourred, which is my case since the logic stems from a Database Trigger event.

This is however one possible way of doing it, I guess there are other ways you could implement it but that's the first that came to my mind.

Hope to be helpful.

2
  • or simply use outbound messages....:)
    – Eric
    Feb 4, 2017 at 7:14
  • that have no logging/on which you have little control over....... your choice :)
    – MLucci
    Apr 6, 2017 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.