How to Populate a Value for the External Id Field

When executing a data deployment with Salesforce upsert, you need an external Id field in each of the objects from which you want to deploy records so that duplicates are not created in the target org. Additionally, you have to populate a value for this field in each of the records that you are going to deploy.

This might seem like a lot of work, but actually, you can easily do this by using a simple process builder or an Apex script.

Note: This process can now be easily performed with Copado’s automatic record matching functionality which is available from Summer ‘20 (v16.5). For more information about this, check out the article Automatic Record Matching.

Create a Process Builder

You can create a process builder that updates the external Id field in an object’s record if that field is empty, whenever a record is created or edited. Then, you can execute a simple Apex script so that all existing records under that object are updated.

In this example, we are creating a process builder for the Account object so that when an Account record is created or updated, a value is added to the external Id field. 

To create the process builder, follow the steps below:
  1. Navigate to Setup > Process Builder and click on New:

  2. Give your process a name and a description if desired.
  3. Select a record changes from the The process starts when picklist field:

  4. Choose the object for which you want to create the process, the Account object in this case, and choose when a record is created or edited since the external Id field should be populated both in new and existing records:

  5. Once you have defined the object, you need to add the criteria. In this case, the process should be triggered if the external Id field is empty, so you can set the condition as follows:

  6. Next, you need to add the action. You can choose to populate the external Id field with the same value as the Account Id field, so you can set the action as follows:

  7. Once you are done creating the process, click on Activate to activate your process builder:

    ⓘ Please note that you will need to create a process builder for each of the objects you want to move records from.
  8. Finally, execute the following Apex script to update all account records:
    1. Navigate to the Developer Console.
    2. Click on Debug and select Open Execute Anonymous Window from the menu.
    3. Insert this script update [Select Id From Account]; in the Enter Apex Code window.
    4. Click on Execute:

When all records get updated, the process builder will add the External Id value for each record.
ⓘ  Please note that you will need to add more strings to the script to update other object records, i.e. update [Select Id From Contact]; update [Select Id From Opportunity]; update [Select Id From OpportunityLineItem]; update [Select Id From Product2]; and so on.

Create an Apex Script

If you have a bit more knowledge of Apex code, you can skip the process builder and create an Apex script.
Navigate to the Developer Console and enter the following script in the Enter Apex Code window in Open Execute Anonymous Window:

List<Account> accounts = [Select Id from Account where External_Id__c = ‘’];
for (Account account : accounts) {
     account.External_Id__c = account.Id;
update accounts;


ⓘ  Please note that you must add more of the same logic for each of the different objects for which you want to update the records.

Additionally, if you want to add a bit of customization to the value that will be populated in the external Id field, you can use the script below:

List<Account> accounts = [Select Id, Name from Account where External_Id__c = ‘’];
for (Account account : accounts) {
     account.External_Id__c = account.Name + ‘-’ + account.Id;
update accounts;

This script will populate an external Id field that includes the name of the account + the account Id. This value is more human readable.

How did we do?