Skip to content

Latest commit

 

History

History
69 lines (50 loc) · 4.09 KB

salesforce-integration.md

File metadata and controls

69 lines (50 loc) · 4.09 KB

Detailed Plan for Salesforce Integration

This is a detailed approach on how I plan for Salesforce integration

Part 1: Salesforce API Integration in Subscriber Class

  1. I'll Create a Salesforce Integration Subscriber Class:

    • I'll create a new class named SalesforceCustomerSubscriber that will inherit from SubscriberBase class (for integration utility and key_to_data mapping) exclusively for Salesforce integration. This class will manage all interactions with Salesforce.
  2. Authentication and Client Setup:

    • I'll utilize the simple-salesforce library to establish Salesforce API authentication.
    • I'll initialize the Salesforce client with our Salesforce credentials, including the username, password, security token, consumer key, and consumer secret.
    from simple_salesforce import Salesforce
    sf = Salesforce(
        username='username',
        password='password',
    )
  3. Data Retrieval from Salesforce:

    • I'll use Salesforce API calls to query custom objects in Salesforce.
    • I'll retrieve the necessary records from our Salesforce custom object using SOQL (Salesforce Object Query Language).
    # Query custom objects
    result = sf.query("SELECT Id, Name FROM Your_Custom_Object__c")
    records = result['records']
  4. Data Transformation:

    • I'll transform the Salesforce data into a format that aligns with our Django model's schema.
    • I'll take advantage of the map_data_to_fields and map_fields_to_data fields within the SubscriberBase class for seamless data conversion.
  5. Synchronization:

    • I'll implement functionality to update our Django database and other relevant systems when Salesforce custom object data changes.
    • I'll use function overriding in our Django model and use MainClass to pass updated data to SubscriberClasses.
    • I'll maintain a field_to_key_mapping dictionary within our SalesforceCustomerSubscriber class for field mapping.
    • I'll implement methods within our SalesforceCustomerSubscriber class to push data from our internal system to Salesforce.

Part 2: Handling Incoming Data

  1. Webhook Setup:

    • I'll configure a webhook endpoint within our Django application, providing a URL for Salesforce to send notifications to. We'll ensure that the endpoint supports HTTPS.
  2. Salesforce Workflow Rule:

    • I'll create a Salesforce Workflow Rule that defines when changes in custom objects trigger the webhook.
    • Within the rule, I'll set up an immediate workflow action to send an outbound message.
  3. Processing Webhook Data:

    • In the view defined in views.py for our webhook endpoint, I'll process the incoming data sent by Salesforce. Salesforce typically sends notifications in XML format but we will use JSON as it will be easier to implement.
    • I'll extract relevant information and perform validation to determine whether the data should be saved in our internal database (to stop duplicate records).
  4. Validation and Unsubscription:

    • To prevent infinite loops, I'll validate that we aren't saving an already saved object, and when our origin is Salesforce itself, then I'll unsubscribe the SalesforceCustomerSubscriber class to avoid unnecessary API calls.

Part 3: Error Handling and Security

  1. Error Handling:

    • I'll implement error handling within our webhook code to manage issues such as network errors, invalid payloads, or endpoint unavailability.
    • We'll consider sending events that aren't handled correctly to a dead letter queue for further analysis. This can be done easily as in Celery; we have access to result objects.
  2. Logging:

    • I'll make sure to maintain detailed logs for each webhook interaction, including successful syncs and any errors encountered. Proper logging is essential for debugging and auditing. For this, I will use the logging library.
  3. Security:

    • I'll ensure that our webhook endpoint is secure and protected against unauthorized access. If necessary, we'll use authentication mechanisms like only data from Django applications will be accepted by using ALLOWED_HOST settings.