Webhooks in Salesforce - How to Setup and Configure for Integration | LWC Stack


 Hi there, in this blog we will learn about webhooks. In simple words webhooks are a kind of mechanism to alter system from another system.

Let's understand it using an example - Let's say there are two servers "Server A" and "Server B".

Server A is the source from where we need to send the data and Server B is the target destination for the data. But there is another server database "Server C" in between them, so in end to end integration Server A will be sending data to Server C and Server B will be fetching it from Server C.

To fetch the data Server B will be hitting API request to Server C and will check if the data is available then fetch the data or else return null. Now without webhooks Server B will be continuously hitting API after a timespan to check if Server C is having the data which is not an efficient way as each API will be costing you something.

As a solution we will create a webhook URL and will give it to Server C and tell it that whenever you are having data just send Server B a ping.

Now whenever Server B is receiving a ping it will hit the request to Server B and will fetch the data.

Follow below steps to create a webhook in Salesforce


Step 1: Create Apex Webhook Handler

WebhookHandler.cls

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
@RestResource(urlMapping='/api/invokeWebhook/*')
global with sharing class WebHookHandler {
    @HttpPost
    global static void handlePost(){
        RestRequest requestContext = RestContext.request;
        RestResponse responseContext = RestContext.response;
        //Request
        //Do something when notification received
        //For demo I have created an Account
        Account acc = new Account(Name='Webhook Account 1');
        insert acc;
    }
}


Step 2: Go to Setup --> Sites --> Create a new Site with default configurations. Choose any Active Site Home Page as it's required and Save it. 



Step 3: In Site Detail click on Public Access Settings button and provide access to apex class.



Step 4: Create Named & External Credentials with required auth type. For this demo I have selected No Authentication method. 
Copy and paste your site url as 

Site URL + /services/apexrest/api/invokeWebhook




Step 5: Test the webhook functionality using Postman with a POST request



Step 6: Validate the webhook functionality by checking the desired outcomes. In above example we created below Account.



Output



Checkout complete video tutorial below

 If you have any question please leave a comment below.

If you would like to add something to this post please leave a comment below.
Share this blog with your friends if you find it helpful somehow !

Thanks

Happy Coding :)

Post a Comment

0 Comments