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

1

Prepare Webhook Data

Before setting up the integration, ensure you have the following details:

1

Thnks API URL

https://api.thnks.com/api/v1/gift_requests

2

HTTP Method

POST

3

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:

{  
  "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.

4

Headers

You will need to add the following headers in the API call:

  • Content-Type: application/json

  • Authorization: Your Thnks API key (Authorization Token).

2

Create an Apex Class for the API Callout

1

Navigate to Setup in Salesforce

Go to Setup and search for Apex Classes.

2

Create a New Apex Class

Write an Apex class for making the API callout to the Thnks endpoint. Below is an example Apex class:

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', 'Bearer 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.

3

Trigger the Apex Class

1

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).

2

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:

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.

4

Test and Verify

1

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.

2

Check Logs

Verify the API callout response in the Apex Logs to ensure the gift was sent successfully.

3

Make Adjustments if Necessary

Update your Apex class, parameters, or process settings based on test results.

5

Best Practices

1

Error Handling

Add error handling in your Apex class to manage failed API callouts.

2

Security

Store your API key securely using Custom Metadata Types or Named Credentials in Salesforce.

3

Testing

Test the integration thoroughly to ensure tokens, parameters, and triggers function as intended.

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

References