# Salesforce Legacy Export Integration You can connect Salesforce with Treasure Data for greater control over your Salesforce data and better integration with the other business applications in your marketing and sales operations stack. Integrating Salesforce with Treasure Data makes it easy to: - **Add new features to Salesforce.** For example, you can prevent churn by tracking web usage and receiving alerts when customers’ product usage declines. - **Use Salesforce data to improve other parts of your marketing stack.** For example, you can increase your Facebook Ads ROI by automatically removing new customers from your Facebook Custom Audiences. For sample workflows on exporting data from Salesforce, go to [Treasure Boxes](https://github.com/treasure-data/treasure-boxes/tree/master/td/sfdc). If you don’t have a Treasure Data account yet, [contact us](https://www.treasuredata.com/contact_us/) so that we can get you set up. A front-end application streams data to be collected in Treasure Data via Log/data collector daemon or Mobile SDKs. You can also bulk import your data using Bulk Import from the CLI. A scheduled query is setup in Treasure Data to run periodically on the data and write the result of each query execution into your Salesforce.com Object. The following is a fairly common architecture, for example: ![](/assets/image-20201023-225746.411557329ca7ac467dcadc71de81738abc53ae3fccba162458a3951377529c8e.0ef47029.png) ## Ranking: What are the “Top N of X?” Every social/mobile application calculates the “top N of X” (ex: top 5 movies watched today). Treasure Data already handles the raw data warehousing; the [write-to-salesforce.com](http://write-to-salesforce.com/)” feature enables Treasure Data to find the “top N” data as well. ## Dashboard Application If you’re a data scientist, you need to keep track of a range of metrics every hour/day/month and make them accessible via visualizations. Using this “[write-to-Salesforce.com](http://write-to-salesforce.com/)” feature, you can streamline the process and focus on building visualizations of your query results via Reports and Dashboards on the Salesforce.com organization. ## Prerequisites - Basic knowledge of Treasure Data, including the [TD Toolbelt](https://toolbelt.treasuredata.com/). - Salesforce.com organization and username, password, and security token for API integration. - You have “API Enabled” permission. - Target Salesforce.com Object should exist with read and write permissions. # Supported Authentication method SFDC output supports these authentication types: - Credential - Session # Use the TD Console to Create Your Connection ## Create a New Connection Connecting to Salesforce using the TD Console is quick and easy. Alternatively, create a Salesforce connection using the command line. The export integration supports credentials; you need a client ID and client secret to authenticate using credentials. ## Allow TD to Access Salesforce using Salesforce These instructions guide you to locate the client ID and client secret that is necessary to authenticate using credentials. his is the guide for Lightning Experience UI. 1. Go to **Setup** >**Apps** > **App Manager**. 2. Select **New Connected App.** 3. Set some values and check Enable OAuth Settings. Then, grant OAuth permission scope. ![](/assets/oauth_setting.7fc14f2db75ac78c8f2ac3de705e2fab32f3eb4f5209915dc51d425433ace4aa.9939779b.png) 4. Click **Save**. Activation can take up to 10 minutes. 5. Go to **Setup** > **Apps** > **App Manager**. 6. Locate the connected app, click **▼**, and then select **View**. ![](/assets/connected_app.9e63c75823ffc511c1015194c179433b8099f9f358711098a1699ee6e4f1bed8.9939779b.png) 7. Write down or copy your **Consumer Key** (client_id) and **Consumer Secret** (client_secret). ![](/assets/crient_secret.34a2b6324e27f53438e82f7a396c84b094c94c190bc093a92ddbf504fc0c7bd3.9939779b.png) 8. For secure account access, get a Salesforce Security Token. If you don’t have a security token, go to **Account** > **Settings** > **Reset My Security Token** and select **Reset Security Token.** You’ll receive your Security Token by email. In Spring 19 classic UI: **My account** > **My Settings** > **Personal** > **Reset My Security Token**. ## Use TD Console ### Create New Connection in TD Console 1. Open **TD Console**. 2. Navigate to **Integrations Hub** > **Catalog**. 3. Search for **Salesforce**. ![](/assets/image2021-5-20_13-37-37.ea3e276f3d1c7287bb5c1f2c0e4d3b9220f1ce99e54b863e7353dbe50cf6b5e0.0ef47029.png) 4. To authenticate with Credentials 1. To authenticate with your credentials, enter your username (your email) and password, as well as your Client ID, Client Secret, and Security Token. 2. In the dialog box, enter [login.salesforce.com](https://login.salesforce.com/) as the login URL. Remove unnecessary letters from Login URL parameter.![](/assets/image-20200703-001720.af452b1f9ec571b64403178a495107a980e547d907f48f30614cf4b53843e7ed.0ef47029.png) 3. Select **Continue.** 4. Give your connection a descriptive name and select **Create Connection.** ### Validate Your Salesforce Connection To validate the user permission, make sure to use Salesforce to validate: - Authority: check the Salesforce export integration connection steps. - Allowed access to Salesforce from Treasure Data: sometimes configuring this requires knowing and configuring your TD static IP address. Please contact support if you require the static IP address information. Without properly configured authority and access, you might encounter access restriction errors. For example: ``` Response not 2xx: 400 Bad Request {"error":"invalid_grant","error_description":"authentication failure"} ``` Define your Query Select a database and table from the popup menus. Enter your query and select Output results. To avoid any issues with result export, define column aliases in your query such that resulting column names from the query match the Salesforce field names for default fields and API names (usually ending with __c) for custom fields. ### Integration Parameters for Salesforce ![](/assets/image2021-5-20_13-44-4.9d012ff1074ecec7fb8ebf67e6c03037b9c1eddb676242d774f81a788b26645d.0ef47029.png) | Parameter | Description | | --- | --- | | Object | Salesforce.com Object | | Mode | - The append mode is the default that is used when no mode option is provided in the URL. In this mode, the query results are appended to the object. - With the truncate mode, the system first truncates the existing records in the Salesforce.com Object and moves them into the Trashbin, then inserts the query results. - With the update mode, a row is inserted unless it would cause a duplicate value in the external key columns specified in the “unique” parameter. In such a case, an update is performed instead. The “unique” parameter is required with this mode and must be defined as an external key when used with the update mode. | | Concurrency mode | The concurrency_mode option controls how the data is uploaded to the Salesforce.com organization. The default mode is parallel. With the parallel method, data is uploaded in parallel. This is the most reliable and effective method for most situations. if you see “UNABLE_TO_LOCK_ROW” in an error message, try `concurrency_mode=serial` instead. | | Retry Limit | This option sets the number of attempts made to write the result to the configured Salesforce.com destination if errors occur. If the export fails more than the set number of retries, the query fails. The default number of retries is `retry=2`. | | Batch size | Splits the records in the result of a query in chunks of 10000 records by default and bulk uploads one chunk at a time. | ### Example Query ![](/assets/image2021-5-20_13-20-31.8344be45d149803e97d943a8a34d6a1b98dc5e4ec54405258b1625b23f51dc0c.0ef47029.png) ```sql SELECT identifier_type, identifier FROM table my_table ``` ### (Optional) Schedule Query Export Jobs You can use Scheduled Jobs with Result Export to periodically write the output result to a target destination that you specify. Treasure Data's scheduler feature supports periodic query execution to achieve high availability. When two specifications provide conflicting schedule specifications, the specification requesting to execute more often is followed while the other schedule specification is ignored. For example, if the cron schedule is `'0 0 1 * 1'`, then the 'day of month' specification and 'day of week' are discordant because the former specification requires it to run every first day of each month at midnight (00:00), while the latter specification requires it to run every Monday at midnight (00:00). The latter specification is followed. #### Scheduling your Job Using TD Console 1. Navigate to **Data Workbench > Queries** 2. Create a new query or select an existing query. 3. Next to **Schedule**, select None. ![](/assets/image2021-1-15_17-28-51.f1b242f6ecc7666a0097fdf37edd1682786ec11ef80eff68c66f091bc405c371.0f87d8d4.png) 4. In the drop-down, select one of the following schedule options: ![](/assets/image2021-1-15_17-29-47.45289a1c99256f125f4d887e501e204ed61f02223fde0927af5f425a89ace0c0.0f87d8d4.png) | Drop-down Value | Description | | --- | --- | | Custom cron... | Review [Custom cron... details](#custom-cron-details). | | @daily (midnight) | Run once a day at midnight (00:00 am) in the specified time zone. | | @hourly (:00) | Run every hour at 00 minutes. | | None | No schedule. | #### Custom cron... Details ![](/assets/image2021-1-15_17-30-23.0f94a8aa5f75ea03e3fec0c25b0640cd59ee48d1804a83701e5f2372deae466c.0f87d8d4.png) | **Cron Value** | **Description** | | --- | --- | | `0 * * * *` | Run once an hour. | | `0 0 * * *` | Run once a day at midnight. | | `0 0 1 * *` | Run once a month at midnight on the morning of the first day of the month. | | "" | Create a job that has no scheduled run time. | ``` * * * * * - - - - - | | | | | | | | | +----- day of week (0 - 6) (Sunday=0) | | | +---------- month (1 - 12) | | +--------------- day of month (1 - 31) | +-------------------- hour (0 - 23) +------------------------- min (0 - 59) ``` The following named entries can be used: - Day of Week: sun, mon, tue, wed, thu, fri, sat. - Month: jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec. A single space is required between each field. The values for each field can be composed of: | Field Value | Example | Example Description | | --- | --- | --- | | A single value, within the limits displayed above for each field. | | | | A wildcard `'*'` to indicate no restriction based on the field. | `'0 0 1 * *'` | Configures the schedule to run at midnight (00:00) on the first day of each month. | | A range `'2-5'`, indicating the range of accepted values for the field. | `'0 0 1-10 * *'` | Configures the schedule to run at midnight (00:00) on the first 10 days of each month. | | A list of comma-separated values `'2,3,4,5'`, indicating the list of accepted values for the field. | `0 0 1,11,21 * *'` | Configures the schedule to run at midnight (00:00) every 1st, 11th, and 21st day of each month. | | A periodicity indicator `'*/5'` to express how often based on the field's valid range of values a schedule is allowed to run. | `'30 */2 1 * *'` | Configures the schedule to run on the 1st of every month, every 2 hours starting at 00:30. `'0 0 */5 * *'` configures the schedule to run at midnight (00:00) every 5 days starting on the 5th of each month. | | A comma-separated list of any of the above except the `'*'` wildcard is also supported `'2,*/5,8-10'`. | `'0 0 5,*/10,25 * *'` | Configures the schedule to run at midnight (00:00) every 5th, 10th, 20th, and 25th day of each month. | 1. (Optional) You can delay the start time of a query by enabling the Delay execution. ### Execute the Query Save the query with a name and run, or just run the query. Upon successful completion of the query, the query result is automatically exported to the specified destination. Scheduled jobs that continuously fail due to configuration errors may be disabled on the system side after several notifications. (Optional) You can delay the start time of a query by enabling the Delay execution. ## Activate a Segment in Audience Studio You can also send segment data to the target platform by creating an activation in the Audience Studio. 1. Navigate to **Audience Studio**. 2. Select a parent segment. 3. Open the target segment, right-mouse click, and then select **Create Activation.** 4. In the **Details** panel, enter an Activation name and configure the activation according to the previous section on Configuration Parameters. 5. Customize the activation output in the **Output Mapping** panel. ![](/assets/ouput.b2c7f1d909c4f98ed10f5300df858a4b19f71a3b0834df952f5fb24018a5ea78.8ebdf569.png) - Attribute Columns - Select **Export All Columns** to export all columns without making any changes. - Select **+ Add Columns** to add specific columns for the export. The Output Column Name pre-populates with the same Source column name. You can update the Output Column Name. Continue to select **+ Add Columns**to add new columns for your activation output. - String Builder - **+ Add string** to create strings for export. Select from the following values: - String: Choose any value; use text to create a custom value. - Timestamp: The date and time of the export. - Segment Id: The segment ID number. - Segment Name: The segment name. - Audience Id: The parent segment number. 1. Set a **Schedule**. ![](/assets/snippet-output-connector-on-audience-studio-2024-08-28.a99525173709da1eb537f839019fa7876ffae95045154c8f2941b030022f792c.8ebdf569.png) - Select the values to define your schedule and optionally include email notifications. 1. Select **Create**. If you need to create an activation for a batch journey, review [Creating a Batch Journey Activation](/products/customer-data-platform/journey-orchestration/batch/creating-a-batch-journey-activation). ## Optionally Configure Export Results in Workflow Within Treasure Workflow, you can specify the use of this data connector to export data. Learn more at [Exporting Data with Parameters](https://docs.treasuredata.com/smart/project-product-documentation/exporting-data-with-parameters). ### Example Workflow for Salesforce Export ```yaml timezone: UTC +td-result-output-sfdc: td>: queries/sample.sql database: sample_datasets result_connection: your_connection_name result_settings: object: object_name mode: append concurrency_mode: parallel retry: 2 split_records: 10000 ```