How use Salesforce External Ids with Heroku Connect

By

3 minutes de lecture

Heroku Connect

Heroku Connect is part of the Heroku platform used to automatically synchronize data between Salesforce and the Heroku PostgreSQL database.

Depending on your settings, when data is inserted, modified or deleted in Salesforce, Heroku Connect duplicates it in Postgre. Conversely, when data is inserted in Postgre’s tables, Heroku Connect duplicates it in Salesforce.

Salesforce Lookup

The Salesforce Objects structure offers a Lookup field type that is used to connect two object records together. The lookup fields contains IDs created and managed by Salesforce . You can’t create ID values from an external system.

For example :

If an Object (B) has a relation to another Object (A), you can use the lookup field in Object (B) to relate it to (A) by putting record “a” ID in lookup field in record “b”.

Relationship between two objects via relationship External IDs

Unfortunately when you create Salesforce records in an external system (Postgre database in our case) you don’t immediately have access to Salesforce records IDs.

Salesforce solves this problem by allowing the child record insertion to refer to the parent record via an External ID that is inserted with the parent record.

Heroku Connect allows you to set an additional relationship field in the child record. This field is automatically created by Heroku Connect, as soon as there is a lookup link to a parent object that has an external Id, named as a combination of Salesforce ID reference/lookup field and foreign External ID you will be using.

According to the example set out above, let’s now assume that in Salesforce :

  • You’ve configured an External ID field called External_ID__c on the object A.
  • Object B has a standard lookup to Object A through the ObjectAId__c.

In Heroku Connect settings you will be automatically offered the possibility to add the field ObjectAId_rExternal_ID_c in Object B mapping page which is the relationship field you can use to relate B to A.

1. Objects structure in Salesforce

Objects structure in Salesforce

2. Heroku Connect Setting for Object B

Heroku Connect : Edit Mapping of Object B
Edit Mapping of Object B

3. Object B table structure in PostgreSQL database

Heroku connect adds column to table structure
Heroku connect adds column to table structure

How to populate these fields

You need two steps to set a relation between B and A :

Step 1 : Create Object A record in Postgre :

  • With SQL :

https://gist.github.com/mguemmar/98fab10e58716b588b63002e1365fdd8

Nota Bene : External IDs must be unique and unchanged over time like Salesforce IDs. Salesforce recommends using random UUIDs which can be generated using uuid_generate_v4() or gen_random_uuid() in postegre or UUID.randomUUID() in java

  • With Java : Assume we use JPA Repositories to manage database

https://gist.github.com/mguemmar/e4668ac792dad092447e44507c4beb1e

Step 2 : Create Object B record and link it to A in Postgre :

  • With SQL : Assuming the database table primary key id was captured from the previous insert, the Object B record can be inserted as below

https://gist.github.com/mguemmar/b7ef137ab1436729a152645d5f6e79ce

  • With Java :

https://gist.github.com/mguemmar/fabc4c41059d825ec5e498b0b811e953

This relationship will now be accurately populated in Salesforce once Heroku Connect processes these insertions.

Thanks for reading! Feel free to leave a comment…

Links :

https://devcenter.heroku.com/articles/writing-data-to-salesforce-with-heroku-connect#simple-relationships-between-two-objects-and-relationship-external-ids

https://devcenter.heroku.com/articles/heroku-connect#unique-identifier

Read more posts

Enforce code standards with PMD

Developers working on a project usually set coding rules to have a standardized codebase. It is an important piece of the code maintainability, and it can be very easy …
March 2023
Advices
Scratch orgs

Uncovering Salesforce Settings: A Step-by-Step Guide for Scratch Orgs

Today, it’s pretty easy to build your Scratch Org definition file when you know what Settings you want to activate, as they are mapped with the same setting names …
February 2023
Advices
Business Analyst

Core qualities of a Business Analyst?

A common definition we are used to hear is that being a Business Analyst means to have a combination of both hard skills and soft skills. What does a …
June 2022
Advices
Image d'illustration d'une employée travaillant sur un ordinateur portable

Process builder and workflow make way to Flows (½)

Overview “If you can do it with a Workflow, then do it with a Process Builder, because everything a Workflow does, a Process Builder does it better”. If you …
March 2022
Advices

Day 22 : Salesforce new “Migrate To Flow tool” in Spring 22

As most of you already know, the workflow rules and process builders are planned to be retired in 2023 (no precise date defined so far). Today, I’m going to …
December 2021
Advices

Day 18 : Fake callout responses for test classes !

Hello everybody ! Today let’s talk about Apex tests classes in Salesforce. Everyone loves a good test class, and Salesforce makes it official by requiring to have a minimum …
December 2021
Advices