Accueil > Conseils > How use Salesforce External Ids with Heroku Connect

How use Salesforce External Ids with Heroku Connect

 3 minutes de lecture

AUTEUR
DATE

mai 25, 2021

CATEGORIES
HASHTAGS
PARTAGEZ !
ABONNEZ-VOUS :

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

2. Heroku Connect Setting for Object B

Edit Mapping of Object B

3. Object B table structure in PostgreSQL database

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