Skip to contentSkip to navigationSkip to topbar
Page tools
Useful for sharing or LLM
Accelerate development with AI

On this page
Looking for more inspiration?Visit the

RETL Queries for Importing Salesforce Objects Into Unified Profiles


You can use the following Reverse ETL (RETL) SQL queries to convert Salesforce objects with US phone number patterns (typically (555) 231-7654) into Segment Unify profiles with E.164 phone number formats (+15552317654). Unified Profiles in Flex requires phone numbers used for profile lookups to be in E.164 format.

Segment created three sample queries for Unified Profiles users to import common Salesforce objects:

  • Accounts
  • Contacts
  • Leads

To selectively import columns, replace the a*, c*, or l* with a list of fields to selectively import. For example, if you wanted to only import the ID, NAME, and ADDRESS fields for your Accounts, you'd replace a* with a.ID, a.NAME, a.ADDRESS.

(success)

Success!

Segment creates a default database table (segment_flex_unify) during the Segment for Flex setup process.


Accounts

accounts page anchor

To import Salesforce Accounts into Unified Profiles as Segment Unify profiles, create a RETL mapping with the following format.

Replace <database_name.account_table> with your database name and account table, PHONE with the name of the column in your warehouse that contains phone numbers, and BILLING_COUNTRY with the name of the column in your warehouse that contains the account's country. In this sample query, the PHONE column is the primary contact phone number and what you import as the Profile's 'phone' trait.

The other phone fields for Salesforce Accounts are PersonHomePhone, PersonMobilePhone, & PersonOtherPhone, and could be substituted for PHONE in this query. You can only import one phone number field as the identifier used for lookups in Unified Profiles.

1
SELECT
2
a.*,
3
CASE
4
WHEN a.PHONE REGEXP '^(\\([0-9]{3}\\) [0-9]{3}-[0-9]{4})$' AND a.BILLING_COUNTRY = 'US'
5
THEN CONCAT('+1', REGEXP_REPLACE(a.PHONE, '[^0-9]',''))
6
WHEN a.BILLING_COUNTRY != 'US'
7
THEN REGEXP_REPLACE(a.PHONE, '[^0-9]','')
8
ELSE a.PHONE
9
END as phone,
10
FROM
11
<database_name.account_table> a
12
WHERE
13
a.PHONE IS NOT NULL
14
AND a.BILLING_COUNTRY IS NOT NULL;

After running this query, you can use 'phone' for lookups in Unified Profiles.


To import Salesforce Contacts into Unified Profiles as Segment Unify profiles, create a RETL mapping with the following format.

Replace <database_name.contact_table> with your database name and account table, PHONE with the name of the column in your warehouse that contains phone numbers, and BILLING_COUNTRY with the name of the column in your warehouse that contains the contact's country.

Salesforce objects have several phone number-related fields. In this sample query, the PHONE column is the primary contact phone number and what you import as the Profile's 'phone' trait.

The other phone fields for Salesforce Contacts are HomePhone, MobilePhone, & OtherPhone, and could be substituted for PHONE in this query. You can only import one phone number field as the identifier used for lookups in Unified Profiles.

1
SELECT
2
c.*,
3
CASE
4
WHEN c.PHONE REGEXP '^(\\([0-9]{3}\\) [0-9]{3}-[0-9]{4})$' AND c.MAILING_COUNTRY = 'US'
5
THEN CONCAT('+1', REGEXP_REPLACE(c.PHONE, '[^0-9]',''))
6
WHEN c.MAILING_COUNTRY != 'US'
7
THEN REGEXP_REPLACE(c.PHONE, '[^0-9]','')
8
ELSE c.PHONE
9
END as phone,
10
FROM
11
<database_name.contact_table> c
12
WHERE
13
c.PHONE IS NOT NULL
14
AND c.MAILING_COUNTRY IS NOT NULL;

After running this query, you can use 'phone' for lookups in Unified Profiles.


To import Salesforce Leads into Unified Profiles as Segment Unify profiles, create a RETL mapping with the following format.

Replace <database_name.lead_table> with your database name and lead table, PHONE with the name of the column in your warehouse that contains phone numbers, and BILLING_COUNTRY with the name of the column in your warehouse that contains the lead's country.

Salesforce objects have several phone number-related fields. In this sample query, the PHONE column is the primary contact phone number and what you import as the Profile's 'phone' trait.

The other phone fields for Salesforce Leads are HomePhone, MobilePhone, & OtherPhone, and could be substituted for PHONE in this query. You can only import one phone number field as the identifier used for lookups in Unified Profiles.

1
SELECT
2
l.*,
3
CASE
4
WHEN l.PHONE REGEXP '^(\\([0-9]{3}\\) [0-9]{3}-[0-9]{4})$' AND l.COUNTRY = 'US'
5
THEN CONCAT('+1', REGEXP_REPLACE(l.PHONE, '[^0-9]',''))
6
WHEN l.COUNTRY != 'US'
7
THEN REGEXP_REPLACE(l.PHONE, '[^0-9]','')
8
ELSE l.PHONE
9
END as phone,
10
FROM
11
<database_name.lead_table> l
12
WHERE
13
l.PHONE IS NOT NULL
14
AND l.COUNTRY IS NOT NULL;

After running this query, you can use 'phone' for lookups in Unified Profiles.


If these queries don't return phone numbers in E.164 format, examine your existing data and change the REGEX patterns as appropriate.

Because the format in which an international phone number is saved in Salesforce largely depends on how users input them into the system, Salesforce administrators can add form validation methods, like input field validation rules with REGEXP functions, to guide users to input phone numbers in specific formats. These form validation methods may impact the format of the phone numbers in your database, and might require you to change the REGEXP patterns in the provided queries.