Syncing contacts from Google Cloud Storage to Salesforce

Code

Before getting started make sure to set your Polytomic API key as an environment variable:

$export POLYTOMIC_API_KEY=YOUR-API-TOKEN

This example tutorial covers five steps:

  1. Connecting to Salesforce.
  2. Connecting to Google Cloud Storage.
  3. Verifying connection readiness.
  4. Creating a Google Cloud Storage CSV data model.
  5. Create a Contacts sync from Google Cloud Storage to Salesforce.

1. Create a Salesforce connection

The following request will create a Salesforce connection using the Create Connection endpoint (you should consider using Polytomic Connect’s embedded auth as an easier alternative).

$curl --request POST \
> --url https://app.polytomic.com/api/connections \
> --header "accept: application/json" \
> --header "content-type: application/json" \
> --header "X-Polytomic-Version: 2024-02-08" \
> --header "Authorization: Bearer ${POLYTOMIC_API_KEY}" \
> -d '{"name": "Salesforce Connection","type": "salesforce", "configuration": {"domain": "https://example.my.salesforce.com"}}'

Since Salesforce connections use OAuth to authenticate, we’ll need to follow the link returned in the auth_url parameter in the response.

OAuth Redirection

By default, the API expects the auth_url to be opened in a new browser window. If you’d like to modify the redirect behavior, there is an optional redirect_url parameter that can be added to the request body.

2. Create a Google Cloud Storage connection

The following request will create a Google Cloud Storage connection.

$curl --request POST \
> --url https://app.polytomic.com/api/connections \
> --header "accept: application/json" \
> --header "content-type: application/json" \
> --header "X-Polytomic-Version: 2024-02-08" \
> --header "Authorization: Bearer ${POLYTOMIC_API_KEY}" \
> -d '{"name": "Google Cloud Storage Connection","type": "gcs", "configuration": { "service_account": "service_account_json_key", "bucket": "gcs://mybucket/mypath"}}'

3. Verify connection readiness

Before using either connection for syncing, verify that the Get Schema Status call for each connection ID returns cache_status = true (you can poll the endpoint until you see this).

When this value is set to true, the connections’ schemas are ready to be interrogated.

4. Create a Google Cloud Storage source CSV data model

We will now create a data model from a CSV file on Google Cloud Storage (hereafter referred to as ‘GCS’). Data models are collections of fields (i.e. views) from which you can select any subset to sync to other systems.

We will use the Create Model endpoint to surface all columns from a high_paying_users.csv file on GCS:

$curl --request POST \
> --url https://app.polytomic.com/api/models \
> --header "accept: application/json" \
> --header "content-type: application/json" \
> --header "X-Polytomic-Version: 2024-02-08" \
> --header "Authorization: Bearer ${POLYTOMIC_API_KEY}" \
> -d '{
> "name": "GCS Contacts",
> "configuration": {
> "model_from": "single_file",
> "key": "high_paying_users.csv"
> },
> "connection_id": "YOUR_GCS_CONNECTION_ID"
> }'

5. Create a sync from the above GCS model to Salesforce Contacts

This sync maps the email field from the GCS CSV model to the Email field of Salesforce Contacts. It additionally maps the first_name and last_name fields from the GCS CSV model. The field mappings can be extended to sync additional fields.

Enumerating destination objects

To list the destination objects to sync to, use the Get Sync Target Objects endpoint.

Creating a sync from your GCS CSV model to Salesforce Contacts

We will now build a sync from the GCS CSV model to Salesforce Contacts using the Create Sync endpoint.

$curl --request POST \
> --url https://app.polytomic.com/api/syncs \
> --header "accept: application/json" \
> --header "content-type: application/json" \
> --header "X-Polytomic-Version: 2024-02-08" \
> --header "Authorization: Bearer ${POLYTOMIC_API_KEY}" \
> -d '{
> "name": "GCS to Salesforce Sync",
> "mode": "updateOrCreate",
> "identity": {
> "source": {
> "field": "email",
> "model_id": "YOUR_MODEL_ID"
> },
> "target": "Email",
> "function": "equality"
> },
> "fields": [
> {
> "source": {
> "field": "first_name",
> "model_id": "YOUR_MODEL_ID"
> },
> "target": "FirstName"
> },
> {
> "source": {
> "field": "last_name",
> "model_id": "YOUR_MODEL_ID"
> },
> "target": "LastName"
> }
> ],
> "schedule": {
> "frequency": "Manual"
> },
> "target": {
> "connection_id": "YOUR_SALESFORCE_CONNECTION_ID",
> "object": "Contact"
> }
> }'