Deactivate Salesforce Suite synced Drupal records instead of deleting
To remove a large set of records from a Drupal site whose Salesforce Suite mapping syncs both ways, without deleting anything in Salesforce, don't delete them at all: mark them inactive. Turn off the mapping's push_delete trigger, map Drupal's boolean status to the Salesforce status picklist in both directions with the module's pull and push events, and flip the status in a batched update hook. Salesforce stays the record of history, and Drupal simply stops showing what is no longer current.
The situation
A teammate was cleaning up one site in a group of nonprofit membership sites built on one Drupal distribution. The site keeps contacts, organisations and the relationships between them (who works where) in Drupal, and Salesforce Suite syncs all three with Salesforce in both directions. Over time the site had collected many relationship records that no longer mattered on the website: affiliations with organisations that had stopped being members, affiliations that had ended, and affiliations for contacts who were no longer current. Directory pages listed all of them.
The data in Salesforce was right. In Salesforce, an ended affiliation is not wrong, it is history, and its status picklist already said so. The website just had no notion of "former".
Why deleting was the wrong tool
The team's first version of the cleanup deleted the local records. With a two-way mapping that needs care in three places:
- With
push_deleteon, deleting a Drupal entity queues a delete of the mapped Salesforce record. Removing a few hundred local rows would remove the same rows from the CRM. - With
push_deleteoff but the pull query unchanged, the next pull finds the Salesforce records again and recreates them locally. - Deleting rows also drops their mapped-object records, so if the filter ever turns out too broad, there is no cheap way back.
The revised version keeps the records and deactivates them instead. The relationship entity already had a boolean status field, so "former" could be expressed without a schema change, and deactivation can be undone by flipping the flag back or by a later pull.
Map the status both ways
A boolean in Drupal and a picklist in Salesforce don't map directly through the mapping UI, so the team handled it in an event subscriber. On pull, before the entity is saved, the subscriber reads the Salesforce status and end date and sets the Drupal status. On push, it turns the Drupal boolean back into the picklist value. Here is a minimal version of the idea:
use Drupal\salesforce\Event\SalesforceEvents;
use Drupal\salesforce_mapping\Event\SalesforcePullEvent;
use Drupal\salesforce_mapping\Event\SalesforcePushParamsEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
final class AffiliationStatusSubscriber implements EventSubscriberInterface {
public static function getSubscribedEvents(): array {
return [
SalesforceEvents::PULL_PRESAVE => 'onPullPresave',
SalesforceEvents::PUSH_PARAMS => 'onPushParams',
];
}
public function onPullPresave(SalesforcePullEvent $event): void {
if ($event->getMapping()->id() !== 'affiliation') {
return;
}
$record = $event->getMappedObject()->getSalesforceRecord();
$active = TRUE;
try {
$status = $record->field('Status__c');
$end = $record->field('End_Date__c');
if ($status && $status !== 'Current') {
$active = FALSE;
}
if ($end && strtotime($end) <= time()) {
$active = FALSE;
}
}
catch (\Exception $e) {
// The field isn't on this record; keep it active.
}
$event->getEntity()->set('status', $active ? 1 : 0);
}
public function onPushParams(SalesforcePushParamsEvent $event): void {
if ($event->getMapping()->id() !== 'affiliation') {
return;
}
$active = (bool) $event->getEntity()->get('status')->value;
$event->getParams()->setParam('Status__c', $active ? 'Current' : 'Former');
}
}
With this in place the pull query no longer needs to exclude former records. The team widened it to bring in every affiliation of a current member organisation, current or not, so former ones arrive and land deactivated rather than being left behind as stale active rows. The site's listing views then gained a filter on the relationship's status, which is what actually hides them.
Order the update hook carefully
The one-off cleanup ran as a hook_update_N with a batch sandbox, and the order inside it matters more than the query.
Config first. A standard Drupal deploy runs drush updatedb before drush config:import, so when the update hook runs, the active mapping config is still the old one. The team's hook reads the handful of config objects it depends on (the three mappings and the listing view) straight from sync storage and saves them into active config before touching data, then sets push_delete to false explicitly as a safeguard, and resets the mapping and view entity caches:
$sync = \Drupal::service('config.storage.sync');
foreach (['salesforce_mapping.salesforce_mapping.affiliation', 'views.view.directory'] as $name) {
if ($data = $sync->read($name)) {
\Drupal::configFactory()->getEditable($name)->setData($data)->save(TRUE);
}
}
\Drupal::entityTypeManager()->getStorage('salesforce_mapping')->resetCache();
Then a full re-pull. Salesforce Suite only pulls records modified since the mapping's last pull time, which it keeps in state. Changing the pull query doesn't touch that timestamp, so records that changed status long ago would never be pulled again. Resetting the stored timestamp for that mapping makes the next pull fetch everything the new query matches.
Then find the rows. The set to deactivate came from two places. A database query joined each active relationship to its organisation's membership status, its own end date and its contact's status, treating a missing value as "not current". A SOQL query asked Salesforce for every affiliation whose status isn't Current, paging through the results with queryMore, and the IDs were translated to Drupal entity IDs through the module's mapped-object table. That second query is wrapped so that a local environment without Salesforce credentials still runs the update.
Then deactivate in chunks. The batch sets status = 0 on the base and revision tables a thousand IDs at a time, then resets the entity cache and marks the search index for reindexing. Writing to the tables directly rather than saving each entity is deliberate: an entity save would fire a push for every row, which is exactly the traffic the cleanup wants to avoid.
What I'd watch with this approach
The helper is idempotent: it only selects rows that are still active and should not be. That made it safe for the team to call the same helper from more than one update hook while the rules were being refined, since each run only touches what is left. I like that property for any data clean-up in update hooks; you will rarely get the predicate right the first time.
The trade-off of direct table writes is that they bypass entity hooks. Anything else that reacts to a status change (search, caches, other integrations) needs handling by hand, which is why the reindex and cache resets are part of the hook rather than an afterthought.
Key takeaways
- With a two-way Salesforce Suite mapping, deactivating records is safer than deleting them, because nothing is lost in either system.
- Map a Drupal boolean to a Salesforce picklist in both directions with the PULL_PRESAVE and PUSH_PARAMS events.
- Because updatedb runs before config:import, an update hook that depends on new config should load it from sync storage first.
- Changing a mapping's pull query doesn't re-pull old records; reset the mapping's last pull time to fetch them again.
- Write clean-up update hooks so they can run twice, because the selection rules usually change before the work is done.