By: Urmila Pawar 

Webhooks are special triggers that help transfer data from any system. The data can be in any form (json, xml, text, etc). Webhooks are the modern, web-oriented way for servers to receive notifications from other servers. For instance, when an event happens on a server, your own custom application can receive the event via a web request. Salesforce has a built-in way to handle events called Triggers which run on Salesforce via Apex code. However, you may want to receive these events in your own custom application. 

Webhooks are useful when we need data from a third party and we don’t know the frequency or time of arrival of that data. To receive the data, we create a Rest Resource in Apex class with an endpoint, we then add that endpoint to the third party, the endpoint would be triggered when something happens in the third party.

 

Note: There is no Authentication in Webhook, so we cannot use connected apps for Webhook functionality.

Use Cases of Webhooks in Salesforce:

  1. Receive messages from third parties such as Twilio.
  2. Receive Payment related information from third parties such as PayPal,CCAvenue.
  3. Connect and transfer information between unrelated Systems.
  4. Integrate with third parties like Slack or Facebook to receive their messages in Salesforce.

How to Setup Webhooks in Salesforce?

  1. Create Apex REST API
    1. Create the Apex class that will listen to request from third party
  2. Create a public Site
    1. If you do not have any site already, you can create a new site for exposing the Rest Class created in the earlier step.
  3. Enable Apex class Access to the site
    1. To enable that class go to all sites under setup. Click on the “Public Access Settings” button and navigate to the “Enable Apex Class Access” section and give the permission to your class.
    2. In addition to enabling apex class access, you’ll need to add the appropriate object and field-level permissions for any data you will be exposing; remember that this web service will be accessible without any authentication.

Using the Webhook

    1. The public URL for an Apex REST method has the format {Site_URL}/services/apexrest{URL_mapping}. For example, this will be https://appluent-developer-edition.na9.force.com/services/apexrest/accounts
    2. Add this URL to the third party system in the webhook section and you are all set to receive notifications.
    3. In order to debug the incoming notifications, you can enable the logs for the Guest User associated with the Force.com site which we have used for this webhook.