Migrating portals between orgs

Migrating portals between orgs

A portal is not stored in one place. The metadata (Apex, static resources, VisualForce pages, block definitions) ships with the package, but the actual portals, their pages, and the page and form content are data. Page content, including every form built in the page builder, is stored as Salesforce Files (ContentVersions) linked to the Page__c record, not in fields. A metadata deploy or a package install moves none of it.

Because of this, moving a portal between orgs or sandboxes is a two part data load:

  1. Load the object records: Web_Portal__c, then Page__c, then reconnect the portal to its home and 404 pages.
  2. Load the related ContentVersion files for each page and relink them to the migrated page records.

This article covers both parts and the order they must run in. For the underlying schema see Object model and data dictionary. For how content is compiled and stored see Publishing pages.

What moves how

Not everything is data. Get the platform layer in place first, then load data on top of it.

Layer How it moves Notes
Apex, static resources, VisualForce pages Package install or metadata deploy Do this first. Nothing below works without it.
Portal_Block__mdt, Google_Service_Auth__mdt Metadata deploy (custom metadata) Block palette and push credentials. Deploy, do not data load.
Portal__c (custom setting) Metadata deploy or manual re-entry Org, profile, user level config. Not a transactional object.
Web_Portal__c, Page__c Data load (this article) The portal container and its pages.
ContentVersion files (Template.b3p, Template_html.b3p) Data load (this article) The page and form content. The main event.
Contact (portal users), Web_Event__c, Push_Message__c Usually not migrated Users and activity history are org specific. See the note at the end.

This article assumes the platform layer is already in the target org. It only covers the data load.

Why content lives in files

When a page is saved in the builder, the builder writes two Files against the Page__c record rather than into fields:

Both are gzip plus base64 compressed. A form is not a separate object. Forms are assembled inside a page from field building blocks, so a form's fields, layout, and submit configuration are all part of that page's Template.b3p and Template_html.b3p. Migrate the page files and you migrate the form.

At render time the runtime finds a page's content by matching on the file title and the linked record, not by ID:

SELECT Id, FileType, PathOnClient, CreatedDate, ContentSize
FROM ContentVersion
WHERE ContentDocumentId = :docIdOfLinkedFile
AND IsLatest = true

where the document is found through a ContentDocumentLink whose ContentDocument.Title equals Template.b3p (or Template_html.b3p) and whose LinkedEntityId is the page record.

This is the rule that makes migration work. In the target org each migrated Page__c must end up with files titled exactly Template.b3p and Template_html.b3p linked to it. The record IDs will be different in the target org; the titles must be identical. Get the titles and the links right and the runtime finds the content regardless of new IDs.

The ID problem, and the load order

Two things make the record load non trivial.

First, IDs change across orgs. Every reference has to be remapped. The clean way is to migrate a stable key alongside the record, so the tool can resolve the new parent for you.

Second, Web_Portal__c and Page__c reference each other:

You cannot insert either side with all lookups populated in one pass. Break the cycle by loading in this order:

  1. Insert Web_Portal__c with the home and 404 lookups left blank.
  2. Insert Page__c with Web_Portal__c populated (resolved from the portal you just inserted).
  3. Update Web_Portal__c to set Home_Page__c and Page_404__c now that the pages exist.
  4. Load and relink the ContentVersion files for each page.

Cross org relationship mapping is far easier if each record carries a stable key the loader can match on. If your package build allows it, add a temporary text external ID field (for example Migration_Key__c) to Web_Portal__c and Page__c, populate it in the source, and use upsert on that field in the target. Data Loader then resolves parent references for you and the load is idempotent, so a re run updates rather than duplicates.

If you cannot add a field, you can map on the record Name instead, provided names are unique within the portal, or keep an old ID to new ID mapping file by hand between steps. The steps below note both.

Part one: load the object records

The examples use Data Loader because most admins have it and it handles Files, but the same queries work with the sf CLI or any ETL tool.

Step 1: export from the source org

Portal:

SELECT Id, Name,
       b3p__Branding_Resource_Name__c,
       b3p__PWA_Short_Name__c, b3p__PWA_Description__c,
       b3p__PWA_Start_URL__c, b3p__PWA_Theme_Color__c,
       b3p__PWA_BG_Color__c, b3p__PWA_Status_Bar_Type__c,
       b3p__PWA_Viewport__c,
       b3p__Home_Page__c, b3p__Page_404__c
FROM b3p__Web_Portal__c
WHERE Id = 'a0X...sourcePortalId'

Pages:

SELECT Id, Name, b3p__Web_Portal__c,
       b3p__Title__c,
       b3p__Guest_User_Accessible__c,
       b3p__Disable_Caching__c,
       b3p__Sub_Template__c
FROM b3p__Page__c
WHERE b3p__Web_Portal__c = 'a0X...sourcePortalId'

Keep the source Home_Page__c and Page_404__c values from the portal export; you will need them in step 4.

Step 2: insert the portal

Insert the Web_Portal__c row. Blank out b3p__Home_Page__c and b3p__Page_404__c for this insert; the pages do not exist yet. Also blank b3p__Emulated_Contact__c unless you are migrating that Contact separately.

Record the new portal ID (Data Loader writes a success file with the new IDs). If you added Migration_Key__c, this mapping is automatic on the next step.

Step 3: insert the pages

Map b3p__Web_Portal__c on every page row to the new portal ID from step 2 (or upsert on Migration_Key__c and let Data Loader resolve it).

Keep the Data Loader success file. It gives you the old page ID to new page ID mapping, which you need for both the portal update in step 4 and the file relink in part two.

Step 4: reconnect home and 404

Update the Web_Portal__c row. Set b3p__Home_Page__c and b3p__Page_404__c to the new page IDs, translated from the source values you kept in step 1 using the page mapping from step 3.

At this point the records exist and are wired together, but every page is still blank because no content has been loaded.

Now move the files. For each page you are recreating its Template.b3p and Template_html.b3p as new Files in the target and linking them to the new page record.

Step 5: export the version data from the source

First find the documents linked to the source pages:

SELECT ContentDocumentId, LinkedEntityId,
       ContentDocument.Title,
       ContentDocument.LatestPublishedVersionId
FROM ContentDocumentLink
WHERE LinkedEntityId IN ('a00...srcPage1', 'a00...srcPage2')
AND ContentDocument.Title IN ('Template.b3p', 'Template_html.b3p')

Then export the latest version content for those documents:

SELECT Id, ContentDocumentId, Title, PathOnClient,
       VersionData, FileExtension
FROM ContentVersion
WHERE ContentDocumentId IN ('069...doc1', '069...doc2')
AND IsLatest = true

Data Loader exports VersionData as a separate file per row and puts the file path in the CSV. Keep that export folder intact; the insert reads from it.

Step 6: insert the ContentVersions into the target

Insert into ContentVersion in the target org. For each row set:

Field Value
Title Exactly Template.b3p or Template_html.b3p. Do not change this.
PathOnClient Same as source (for example Template.b3p). Drives the file extension.
VersionData The exported version data for that row.

Do not set ContentDocumentId. Leaving it blank makes each insert create a new ContentDocument in the target, which is what you want. Setting FirstPublishLocationId to the new page ID will both create the document and link it in one step, and if you do that you can skip step 7 for those rows.

Keep the success file. You now have a mapping from source document to new ContentVersion ID and, through it, the new ContentDocumentId.

If you did not use FirstPublishLocationId, create the ContentDocumentLink rows that attach each new document to its new page. This is the step that makes the runtime able to find the content.

Build a CSV that joins:

Insert into ContentDocumentLink:

Field Value
ContentDocumentId New document ID from step 6.
LinkedEntityId New Page__c ID from step 3.
ShareType I
Visibility AllUsers

ShareType = I and Visibility = AllUsers match what the product sets when it saves and shares a template. Guest users must be able to read the file, so AllUsers visibility is required, not optional.

Step 8: verify

Confirm each new page has both files linked with the right titles:

SELECT LinkedEntityId, ContentDocument.Title,
       ContentDocument.LatestPublishedVersionId
FROM ContentDocumentLink
WHERE LinkedEntityId IN ('a00...newPage1', 'a00...newPage2')
AND ContentDocument.Title IN ('Template.b3p', 'Template_html.b3p')
ORDER BY LinkedEntityId

Every migrated page should return two rows, one titled Template.b3p and one titled Template_html.b3p. Then open the portal in the target org. Pages, and any forms on them, should render.

Load order summary

Step Object Operation Key point
1 export Query Keep source home and 404 page IDs.
2 Web_Portal__c Insert Blank the home, 404, and emulated contact lookups.
3 Page__c Insert Point Web_Portal__c at the new portal. Keep the ID mapping.
4 Web_Portal__c Update Set home and 404 to the new page IDs.
5 export Query ContentDocumentLink then ContentVersion, IsLatest = true.
6 ContentVersion Insert Correct Title, blank ContentDocumentId, include VersionData.
7 ContentDocumentLink Insert Link new doc to new page, ShareType = I, Visibility = AllUsers.
8 verify Query Two files per page, titled exactly.

Common mistakes

What this migration does not move

This procedure moves the portal definition and its content only. It deliberately leaves behind: