Create SalesForce Webhook Example Integration

 
import os
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route('/webhook', methods=['POST'])
def salesforce_webhook():
    # Print the data received from Salesforce webhook for verification.
    # Or handle it the way you want to.
    print("Data received from Salesforce webhook:", request.json)
    return jsonify(success=True), 200

if __name__ == '__main__':
    # The server URL will be printed when the app starts.
    server_host = '0.0.0.0'
    server_port = 8080
    print(f"Server listening on: http://{server_host}:{server_port}")
    app.run(host=server_host, port=server_port)

""" With the Salesforce developers account:
1. Create triggers on the Salesforce Objects you want to get updates for
2. Write an Apex class (Webhook) that will send an outgoing message to the LAZY SERVER URL
3. Enable Remote Site Setting for the LAZY SERVER URL you want to send the message to

About this template

The Python server will receive data from a Salesforce webhook and print the data for verification purposes. It will also display the Salesforce webhook URL to confirm where it is listening for incoming requests. Follow the given instructions to setup a Webhook trigger on a SalesForce object.

Introduction to the Salesforce Webhook Integration Template

Welcome to the Salesforce Webhook Integration Template! This template is designed to help you easily set up a Python server that listens for incoming webhook notifications from Salesforce. When a specified event occurs in Salesforce, such as a record being created or updated, Salesforce will send a notification to your Python server, which will then print the data for verification purposes. This is an excellent way to integrate Salesforce events with your applications or workflows.

Getting Started

To begin using this template, simply click on "Start with this Template" on the Lazy platform. This will pre-populate the code in the Lazy Builder interface, so you won't need to copy, paste, or delete any code manually.

Test: Deploying the App

Once you have started with the template, the next step is to deploy the app. Press the "Test" button on the Lazy platform. This will initiate the deployment process and launch the Lazy CLI. The Lazy platform handles all deployment aspects, so you don't need to worry about setting up your environment or installing libraries.

Using the App

After pressing the "Test" button and deploying your app, Lazy will provide you with a dedicated server link. This link is where your Python server will listen for incoming webhook notifications from Salesforce. You can use this link to set up the webhook in Salesforce and to interact with the app.

Integrating the App with Salesforce

To integrate the Python server with Salesforce, follow these detailed steps:

  • Create triggers on the Salesforce Objects you want to get updates for. This is done within the Salesforce platform, under the Object Manager for the specific object you want to monitor.
  • Write an Apex class that will send an outgoing message to your Python server's URL. This Apex class will be responsible for capturing the events and sending the data to your server.
  • Enable Remote Site Settings in Salesforce for your Python server's URL. This step is crucial as it allows Salesforce to send data to your server.
  • Add Secret Verification or another authentication method to your Python server to prevent unauthorized access. This step enhances the security of your webhook endpoint.
  • Ensure that your Apex classes have at least 70% test coverage. Salesforce requires this for deploying code to production.

Here is a sample Apex class that you can use as a starting point:


public class WebhookNotifier {
    @future(callout=true)
    public static void sendNotification(String jsonBody) {
        HttpRequest req = new HttpRequest();
        req.setEndpoint('http://your-python-server-url/webhook');
        req.setMethod('POST');
        req.setHeader('Content-Type', 'application/json');
        req.setBody(jsonBody);
        Http http = new Http();
        HTTPResponse res = http.send(req);
        // Handle the response as needed.
    }
}

Replace 'http://your-python-server-url/webhook' with the server link provided by Lazy after deployment.

Remember to follow Salesforce's documentation for creating triggers and writing Apex classes. You can find more information on these topics in the Salesforce Developer Documentation.

By following these steps, you will have successfully integrated the Salesforce Webhook Integration Template with your Salesforce instance, allowing you to receive and handle events in real-time on your Python server.

Category
Last published
July 20, 2024

More templates like this

MP3ify: Youtube to MP3 Converter

A web application that allows users to download YouTube videos from URLs and provides the option to convert them to MP3 format.

Python
Flask

Discord Moderation Bot

The Discord bot monitors all messages in the server. If a message contains profanity, the bot deletes it and sends a warning to the channel. The bot also notifies the host about the deletion via a direct message. Additionally, the bot outputs a helpful error message to a channel if there is a permissions error, guiding the server admin to enable the required permission in the Discord Developer portal.

Discord
Python
Flask

Backend Server

This skeleton is streamlined for creating backend services using FastAPI. It's an excellent choice for building microservices or APIs with minimal frontend requirements.

Fast API
Python
Home
/
Create SalesForce Webhook Example Integration