> ## Documentation Index
> Fetch the complete documentation index at: https://docs.thnks.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Salesforce

> This integration allows you to set up a process in Salesforce that automatically triggers sending a Thnks via an Apex callout.

# Configuring Salesforce to Automatically Send a Thnks

This guide walks you through automatically triggering the sending of a Thnks using Salesforce Process Builder or Apex triggers.

## Prerequisites

* Access to the Thnks API.
* Thnks API Key (Authorization Token).
* URL for Thnks API endpoint.
* Administrative access to Salesforce for creating custom fields and Apex classes.

## Setup

<Steps>
  <Step title="Prepare Webhook Data">
    Before setting up the integration, ensure you have the following details:

    <Steps>
      <Step title="Thnks API URL">
        `https://api.thnks.com/api/v1/gift_requests`
      </Step>

      <Step title="HTTP Method">
        `POST`
      </Step>

      <Step title="Data (JSON format)">
        Here’s an example of the JSON payload you will need to include in the request. You’ll need to replace the placeholders with the appropriate Salesforce tokens or field variables:

        ```json theme={null}
        {  
          "gift_id": <GIFT_ID>,  
          "recipients": [  
            {  
              "email": "{!Contact.Email}",  
              "first_name": "{!Contact.FirstName}",  
              "last_name": "{!Contact.LastName}",  
              "message": "Thank you for your time and support!",  
              "gift_redeem_type": "email"  
            }  
          ]  
        }  
        ```

        * Replace `<GIFT_ID>` with the Thnks ID of the gift you want to send.
        * Use Salesforce merge fields or dynamic data variables (e.g., `{!Contact.Email}`) to populate lead or contact information.
      </Step>

      <Step title="Headers">
        You will need to add the following headers in the API call:

        * **Content-Type**: `application/json`
        * **Authorization**: Your Thnks API key (Authorization Token).
      </Step>
    </Steps>
  </Step>

  <Step title="Create an Apex Class for the API Callout">
    <Steps>
      <Step title="Navigate to Setup in Salesforce">
        Go to **Setup** and search for **Apex Classes**.
      </Step>

      <Step title="Create a New Apex Class">
        Write an Apex class for making the API callout to the Thnks endpoint. Below is an example Apex class:

        ```apex theme={null}
        public class ThnksGiftSender {  
            @future(callout=true)  
            public static void sendGift(String giftId, String email, String firstName, String lastName, String message) {  
                Http http = new Http();  
                HttpRequest request = new HttpRequest();  
                request.setEndpoint('https://api.thnks.com/api/v1/gift_requests');  
                request.setMethod('POST');  
                request.setHeader('Content-Type', 'application/json');  
                request.setHeader('Authorization', 'YOUR_API_KEY');  
                String requestBody = '{"gift_id": "' + giftId + '", "recipients": [{"email": "' + email + '", "first_name": "' + firstName + '", "last_name": "' + lastName + '", "message": "' + message + '", "gift_redeem_type": "email"}]}';  
                request.setBody(requestBody);  
                HttpResponse response = http.send(request);  
                if (response.getStatusCode() != 200) {  
                    System.debug('Error sending Thnks gift: ' + response.getBody());  
                }  
            }  
        }  
        ```

        Replace `YOUR_API_KEY` with your actual Thnks API Key. Save the class.
      </Step>
    </Steps>
  </Step>

  <Step title="Trigger the Apex Class">
    <Steps>
      <Step title="Use Process Builder or Flow">
        * Create a **Process** in **Process Builder** or a **Flow** in Salesforce to trigger the Apex class.
        * Define the criteria for when the gift should be sent (e.g., Opportunity Stage Closed Won or Lead Status Changed).
        * Use the **Apex Action** to call the `ThnksGiftSender.sendGift` method, passing the required parameters (e.g., Gift ID, Contact Email, First Name, Last Name, and Message).
      </Step>

      <Step title="Use an Apex Trigger (Optional)">
        If you prefer to automate via triggers, you can create a trigger to call the `ThnksGiftSender.sendGift` method based on object changes. Below is an example trigger for a Contact:

        ```apex theme={null}
        trigger ContactTrigger on Contact (after insert) {  
            for (Contact contact : Trigger.new) {  
                ThnksGiftSender.sendGift('GIFT_ID', contact.Email, contact.FirstName, contact.LastName, 'Thank you for your time and support!');  
            }  
        }  
        ```

        Replace `GIFT_ID` with the appropriate Thnks gift ID.
      </Step>
    </Steps>
  </Step>

  <Step title="Test and Verify">
    <Steps>
      <Step title="Run a Test with Sample Data">
        Test the Process Builder workflow, Flow, or Trigger using sample data to ensure the API callout works as expected.
      </Step>

      <Step title="Check Logs">
        Verify the API callout response in the **Apex Logs** to ensure the gift was sent successfully.
      </Step>

      <Step title="Make Adjustments if Necessary">
        Update your Apex class, parameters, or process settings based on test results.
      </Step>
    </Steps>
  </Step>

  <Step title="Best Practices">
    <Steps>
      <Step title="Error Handling">
        Add error handling in your Apex class to manage failed API callouts.
      </Step>

      <Step title="Security">
        Store your API key securely using **Custom Metadata Types** or **Named Credentials** in Salesforce.
      </Step>

      <Step title="Testing">
        Test the integration thoroughly to ensure tokens, parameters, and triggers function as intended.
      </Step>
    </Steps>
  </Step>
</Steps>

Following this process, you can automatically trigger a Thnks gift to be sent as part of a Salesforce workflow using an Apex callout.

## References

* [Apex Callouts Overview](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts.htm)
* [Using Process Builder](https://help.salesforce.com/s/articleView?id=sf.process_overview.htm)
* [Thnks API Documentation](/api-reference/endpoint/create_gift_request)
