Migrating forms between orgs

What you are actually migrating

A form is not stored in one field. It is a small object record plus one or more Files attached to that record. To move a form to another org you move the records and the Files together, in the right order, and repoint the lookups.

Piece Where it lives Notes
Form identity Form__c record Availability window, context object, style lookup
Form definition File Template.3bt on the Form__c Stored as a ContentDocument with one ContentVersion per saved version
Theme File theme_config.json on the Form__c Only the latest version is used
Custom CSS Form_Style__c record Custom_Code__c long text, referenced by Form__c.Form_Style__c

Because the definition and theme are Salesforce Files, this is a ContentVersion migration wrapped around a small record migration. Get the Files wrong and the form record exists but renders nothing.

Prerequisites

Load order

Lookups must exist before the records that point at them, and a File's ContentDocument must exist before you can link it. Load in this order:

  1. Form_Style__c records
  2. Form__c records, with Form_Style__c repointed to the new style Ids
  3. ContentVersion rows for each form's Template.3bt and theme_config.json
  4. ContentDocumentLink rows joining each new ContentDocument to its new Form__c

Step 1: keep a mapping of old Id to new Id

Every insert changes record Ids. The new org will not reuse the source Ids, so you must be able to translate a source Id to its target Id at every step.

The simplest way is to keep a source Id column in every extract and rely on the Data Loader success file, which writes the new Id onto each input row in the same order. After each insert you have a two column map: source Id, target Id.

If you migrate the same forms repeatedly (for example dev to test to prod), add an unprotected External Id text field to Form__c and Form_Style__c, stamp it with the source Id, and use Upsert instead of Insert. That makes every run idempotent and removes the manual join.

Step 2: styles

Export Form_Style__c (Id, Name, Custom_Code__c). Insert into the target. Save the source Id to new Id map.

Step 3: form records

Export Form__c with at least:

Id, Name, Object_Name__c, Form_Style__c, Date_Available_Start__c, Date_Available_End__c

Before insert, replace Form_Style__c with the new style Id from your Step 2 map. Insert, and save the Form__c source to new map. You now have empty form shells with no definition or theme attached.

Step 4: the definition and theme Files

This is the part that most migrations get wrong. A Salesforce File is three objects: a ContentDocument, one ContentVersion per saved version, and a ContentDocumentLink that attaches the document to a record.

Extract the versions

Query the ContentVersions for the forms you are moving. Join through ContentDocumentLink to find the documents linked to each Form__c:

SELECT Id, Title, PathOnClient, VersionData, ContentDocumentId,
       VersionNumber, ReasonForChange, CreatedDate
FROM ContentVersion
WHERE ContentDocumentId IN (
  SELECT ContentDocumentId FROM ContentDocumentLink
  WHERE LinkedEntityId IN ('<sourceFormId>', ...)
)
ORDER BY ContentDocumentId, CreatedDate ASC

VersionData is the file body. Data Loader can export it to files and re-import it by path. Keep the ordering by ContentDocumentId then CreatedDate so versions load oldest first.

Decide how much history you need

Load the versions

For each source document, insert its versions in order:

Map the fields:

Target field Value
Title Template.3bt or theme_config.json
PathOnClient same as Title, sets the file extension
VersionData the exported file body
ReasonForChange carry the source value to preserve the save reason
ContentDocumentId blank for the first version, new document Id for the rest

After each insert, record the map of source ContentVersion Id to new ContentVersion Id. Keep this map. Response migration depends on it entirely.

Inserting the first ContentVersion created a ContentDocument but did not attach it to your Form__c. Insert one ContentDocumentLink per document:

Field Value
ContentDocumentId new document Id
LinkedEntityId new Form__c Id from Step 3
ShareType V
Visibility AllUsers

Step 5: verify

Common failures

Symptom Cause Fix
Form record exists but renders blank Template.3bt not attached Missing or wrong ContentDocumentLink in Step 4
Form renders unstyled theme_config.json missing, or Form_Style__c lookup not repointed Re-link the theme File, fix the style Id in Step 3
Only the latest version shows in history Versions loaded as separate documents Reload with ContentDocumentId chaining so all versions share one document
Style applies to the wrong form Form_Style__c still holds source Ids Repoint using the Step 2 map before inserting Form__c