Agreement account relationship and migration to Client

Agreement account relationship and migration to Client

3B Onboarding is reintroducing a supported relationship between Agreements and Accounts through the Client field, b3o__Client__c, on b3o__Agreement__c.

The original Account field was retired for operational reasons. We recognise that some subscribers and integration partners continue to rely on an Agreement-to-Account relationship. Introducing Client extends support for these implementations and provides a supported field for ongoing integration development.

Client is a new field, rather than a rename of the original Account field. Existing values and integration mappings therefore need to be migrated explicitly.

What changes for subscribers

Field on Agreement Purpose
b3o__Account__c Legacy Account relationship from older versions. Deprecated following the relevant upgrade; may remain in an upgraded subscriber org.
b3o__Client__c New supported lookup to the standard Salesforce Account object. Use this field for new and updated integrations.

Subscribers upgrading from an older version may retain the legacy field and its data in their own org. However, the deprecated field is no longer accessible to the affected packaged code in the b3s and b3o namespaces. A field being visible to a subscriber administrator does not mean that managed-package code can continue to use it.

The migration steps below apply where both fields exist and the subscriber administrator can read the legacy field and update Client. New installations without the legacy field should use Client directly and do not need this backfill or a legacy-field synchronisation Flow.

The 2Cloud9 integration transition

The existing 2Cloud9 integration uses b3o__Account__c. A forthcoming 2Cloud9 release will use b3o__Client__c instead.

Coordinate the 3B Onboarding and 2Cloud9 upgrades with your implementation partner. Confirm which releases contain Client and the corresponding integration change before scheduling production upgrades. This article does not specify release numbers or availability dates.

Copying the data and synchronising the fields does not restore managed-package access to the deprecated field. An older integration that cannot access Account still requires a compatible integration release. The subscriber automation described here bridges data between accessible fields during the transition; it does not replace the 2Cloud9 update.

Prepare the subscriber org

  1. Rehearse the upgrade and migration in a sandbox with representative Agreement data and the relevant integration versions.
  2. Install a 3B Onboarding release that contains Client. In Setup > Object Manager > Agreement > Fields & Relationships, confirm the API names of both fields.
  3. Use an authorised subscriber administrator with permission to execute anonymous Apex, read Agreements and the legacy Account field, and update Client. Grant appropriate Client field access to integration users and other users who need it.
  4. Export Agreement IDs and both relationship fields before migration. Retain this export as the reconciliation and recovery reference.
  5. Identify reports, layouts, Flows, validation rules, Apex, document mappings and external integrations that reference Account. Plan their transition to Client.
  6. Review Agreements where both fields already contain different values. Agree which relationship is correct. The backfill deliberately leaves populated Client values untouched.
  7. Schedule a controlled migration window and pause competing relationship updates while backfilling and reconciling. Agreement updates run existing automation, which may produce downstream notifications or integration activity.

If the legacy field is absent or cannot be queried from subscriber-side Apex, stop and contact your implementation partner or 3B support to establish an export or recovery route. Dynamic Apex does not bypass namespace access restrictions. Do not recreate a field with a similar name and assume that it contains the original data.

Anonymous Apex scripts

Run these scripts in the subscriber org, for example through Developer Console > Debug > Open Execute Anonymous Window, with Open Log selected. They are administrator-run examples, not code to add to either managed package. Anonymous Apex can run without deploying a stored Apex class. See Salesforce: Execute anonymous blocks.

1. Check access and count eligible Agreements

This read-only script checks the fields and counts records with an Account value and a blank Client value. A zero count means there are no records eligible for this backfill; it does not prove that populated values agree.

Schema.DescribeSObjectResult agreementDescribe =
    b3o__Agreement__c.SObjectType.getDescribe();
Map<String, Schema.SObjectField> fieldMap =
    agreementDescribe.fields.getMap();

System.assert(agreementDescribe.isAccessible(), 'Agreement read access is required.');
System.assert(agreementDescribe.isUpdateable(), 'Agreement update access is required.');
System.assert(fieldMap.containsKey('b3o__Account__c'),
    'Legacy Account is unavailable. Confirm the org upgrade history.');
System.assert(fieldMap.containsKey('b3o__Client__c'),
    'Client is unavailable. Confirm the installed 3B Onboarding version.');
System.assert(fieldMap.get('b3o__Account__c').getDescribe().isAccessible(),
    'Legacy Account read access is required.');
System.assert(fieldMap.get('b3o__Client__c').getDescribe().isAccessible(),
    'Client read access is required.');
System.assert(fieldMap.get('b3o__Client__c').getDescribe().isUpdateable(),
    'Client update access is required.');

Integer remaining = Database.countQuery(
    'SELECT COUNT() FROM b3o__Agreement__c ' +
    'WHERE b3o__Account__c != null AND b3o__Client__c = null'
);
System.debug(LoggingLevel.WARN, 'Agreements eligible for backfill: ' + remaining);

2. Preview and copy up to 100 records per execution

Run the access check first. Run the following script with dryRun = true to preview the next group. Review the log, then change dryRun to false to save it. Each execution selects at most 100 eligible Agreements. Successful records no longer meet the selection criteria, so running it again processes the next group.

The script copies Account IDs, not Account names. It does not clear or change the legacy field and does not overwrite an existing Client value. Subscriber automation still runs and may modify fields during the save, so verify persisted values afterwards.

Boolean dryRun = true; // Change to false after reviewing the preview.
Integer batchSize = 100; // Reduce if existing automation needs a smaller scope.

List<b3o__Agreement__c> agreements = [
    SELECT Id, b3o__Account__c, b3o__Client__c
    FROM b3o__Agreement__c
    WHERE b3o__Account__c != null AND b3o__Client__c = null
    ORDER BY Id
    LIMIT :batchSize
];

List<b3o__Agreement__c> changes = new List<b3o__Agreement__c>();
for (b3o__Agreement__c agreement : agreements) {
    changes.add(new b3o__Agreement__c(
        Id = agreement.Id,
        b3o__Client__c = agreement.b3o__Account__c
    ));
    System.debug(LoggingLevel.WARN,
        (dryRun ? 'PREVIEW' : 'ATTEMPT') +
        ' Agreement=' + agreement.Id +
        ' Client=' + agreement.b3o__Account__c);
}

if (dryRun) {
    System.debug(LoggingLevel.WARN,
        'Preview only. Records selected: ' + changes.size());
} else if (changes.isEmpty()) {
    System.debug(LoggingLevel.WARN, 'No eligible Agreements found.');
} else {
    Database.SaveResult[] results = Database.update(changes, false);
    Integer succeeded = 0;
    Integer failed = 0;
    for (Integer i = 0; i < results.size(); i++) {
        if (results[i].isSuccess()) {
            succeeded++;
        } else {
            failed++;
            for (Database.Error err : results[i].getErrors()) {
                System.debug(LoggingLevel.ERROR,
                    'FAILED Agreement=' + changes[i].Id +
                    ' Status=' + err.getStatusCode() +
                    ' Message=' + err.getMessage());
            }
        }
    }
    System.debug(LoggingLevel.WARN,
        'Succeeded=' + succeeded + ', Failed=' + failed);
}

Database.update(changes, false) allows record-level failures to be reported individually while successful records save, provided the transaction completes. An unhandled transaction error, including a governor-limit failure, can still roll back the entire execution. See Salesforce: Database methods and partial success.

After each execution, inspect the error log. Failed records remain eligible and will be selected again. Resolve their validation, access or automation errors before continuing; repeated failures can eventually occupy all 100 positions and prevent progress. If a transaction hits limits, reduce batchSize and investigate the automation involved.

This is manual batching: run the script separately for each group of up to 100. Do not wrap repeated queries and updates in an unlimited loop in one anonymous execution. All work in that execution shares the same transaction limits. For large volumes, ask your Salesforce administrator or partner to implement a subscriber-owned Batch Apex class with equivalent filtering, reconciliation and error handling, using a scope of 100. A real Batch Apex job gives each execution its own transaction. See Salesforce: Use Batch Apex.

3. Verify the migration

Re-run the first script until the eligible count is zero, investigating any persistent failures. Export the following query using your normal Salesforce data export tool:

SELECT Id, Name, b3o__Account__c, b3o__Client__c
FROM b3o__Agreement__c
WHERE b3o__Account__c != null OR b3o__Client__c != null

Compare the two ID columns in the export and reconcile differences against the pre-migration export. Standard SOQL does not support comparing these two field values directly in a WHERE clause. Include Agreements where Client is populated but Account is blank in your review; the backfill does not copy in that direction.

Do not automatically overwrite conflicting values. Resolve them with the data owner, then verify representative Agreements through the user interface and the updated 2Cloud9 integration.

Keep the fields in sync during the transition

Where both fields remain accessible in the subscriber org, create a subscriber-owned before-save record-triggered Flow on b3o__Agreement__c. A before-save Flow assigns values to the triggering record without a separate Update Records operation. See Salesforce: Build a record-triggered Flow.

Configure it as follows:

Use one Flow to own this synchronisation logic, or incorporate it into an existing subscriber Flow responsible for these fields. Validate that the legacy field can be selected and written in Flow Builder before relying on this approach.

Choose the direction and conflict policy

If only legacy systems write the relationship during the transition, Account-to-Client synchronisation is sufficient. Copy on creation when Account is populated and Client is blank, and on updates when Account changes, including when it is cleared. Resolve simultaneous changes explicitly.

If users or integrations can write either field, use the two-way rules below. The recommended conflict policy is that Client wins when both fields change in the same save, because Client is the supported field going forward. Agree this policy with your integration owner before activation.

Create Boolean Formula resources:

IsNewAgreement = ISNEW()
ClientChanged = ISCHANGED($Record.b3o__Client__c)
AccountChanged = ISCHANGED($Record.b3o__Account__c)

Create the resources separately using the formula to the right of each =. First branch on IsNewAgreement. Evaluate the outcomes below in the listed order within each branch and stop after the first matching assignment:

Event Condition Assignment
Create Client is populated Account = Client. This also resolves differing populated values in favour of Client.
Create Client is blank and Account is populated Client = Account.
Create Both are blank No assignment.
Update ClientChanged is true Account = Client, including a null value. This takes priority if both fields changed.
Update ClientChanged is false and AccountChanged is true Client = Account, including a null value.
Update Neither field changed No assignment. Reconcile any pre-existing difference separately.

For example, the Client-to-Account Assignment sets $Record.b3o__Account__c to $Record.b3o__Client__c. The reverse Assignment sets $Record.b3o__Client__c to $Record.b3o__Account__c.

These rules preserve deliberate clearing: clearing one field on update also clears the other. They avoid repopulating a deliberately cleared relationship from an unchanged legacy value. Two-way synchronisation requires both fields to be writable in the subscriber Flow context.

The simultaneous-change policy only applies within one save. Separate updates from competing systems can still replace each other's values. Coordinate ownership so that an older integration does not repeatedly send stale Account values after Client has changed.

Before-save Flows run before Apex before triggers. If a later trigger changes either field, the Flow may not synchronise that later change. Review the existing Agreement save sequence and coordinate with the integration provider where necessary. See Salesforce: Before-save record-triggered Flows.

Test and activate

In the sandbox, test creation using either field, changes through either field, clearing either field, conflicting values supplied together, an unrelated Agreement edit, and bulk updates. Test both the backfill and actual 2Cloud9 transactions with the Flow active. Confirm that validation rules and other automation leave the expected values after the save.

Activating the Flow does not update existing records. Complete the backfill and reconciliation separately. During the production migration window, activate the tested Flow before resuming users and integrations that can write the relationship.

Complete the move to Client

Install the compatible 2Cloud9 release as part of the agreed upgrade sequence, then verify that it reads and writes b3o__Client__c successfully. Update subscriber reports, layouts, mappings and automation to use Client as the primary relationship.

Keep the temporary synchronisation Flow for as long as an accessible legacy process still depends on Account. Once all consumers use Client, retire the legacy synchronisation after testing and dependency review. Do not delete the legacy field while migration, reconciliation or any remaining subscriber dependency still needs it.