2

I have a Drupal install that has been live for many years with over 200 thousand users.

We are currently trying to sync our salesforce users with our Drupal users but we only need to do this for certain user roles which would only be about 3000 or so.

I am using Drupal 9, the Salesforce Suit Module v:5.0.0, and I am using the salesforce_pull submodule that is part of that.

My user mapping is working for importing only the new users we want into Drupal; however, Drupal is creating duplicate users even if a user already exists with the same email as what is being pulled over. About half of those 3000 need to be new, the other half will already exist in the Drupal system.

What I am trying to achieve is for Drupal to find a user with that email, and update that record to be connected with salesforce and update new fields as needed.

I have run into many walls with trying to get this to work and any suggestions would be greatly appreciated.

I have also tried setting the upsert key to be the email; however, that key only seems to apply once the Salesforce connection has been made as it doesn't seem to affect the original binding of the Salesforce object with pre-existing entities.

6
  • This is not possible to answer as written as there is no technical description of the coded solution or contributed modules used. We can't suggest what you should be doing differently because we don't know, technically, what you implemented.
    – mona lisa
    Commented Oct 14, 2022 at 18:48
  • 1
    Help me understand how I need to be clearer... I have listed what module I am using and the mapping that I created and the version - The implementation was that I created a mapping through that module. I am happy to provide more information. In the time being, I have moved the technical details higher up in my question as it was at the bottom and may have been missed Commented Oct 14, 2022 at 19:04
  • 1
    This bug report in the Drupal 7 version of the module appears to address the same question: drupal.org/project/salesforce/issues/2620344
    – mona lisa
    Commented Oct 14, 2022 at 19:24
  • You are not matching the correct upsert key with existing users to their Salesforce ID. That is why its trying to create new users.
    – Kevin
    Commented Oct 14, 2022 at 19:32
  • 1
    @Rubix05 That is more information than is in the question above. When I asked for more information that is the kind of thing that I meant, although you are correct that I overlooked you mentioning the module.
    – mona lisa
    Commented Oct 14, 2022 at 20:54

1 Answer 1

1

The answer was to use an Event Subscriber.

Here is a generic form of my code for anyone else who may have this question. This code is working but there seems to be another event that is in the process of creating a new user which is never completed. I was getting a simple notice log that the user was not able to be created (Which is what I want); however, I would ideally be able to abort this process in the event somehow instead of letting it fail gracefully elsewhere.

Critiques and improvements are welcome!

<?php

namespace Drupal\my_module\EventSubscriber;

use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\salesforce\Event\SalesforceEvents;
use Drupal\salesforce_mapping\Event\SalesforcePullEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Class MyNewSubscriber.
 */
class MyNewSubscriber implements EventSubscriberInterface {

  use StringTranslationTrait;

  public function pullPrepull(SalesforcePullEvent $event) {

    /** @var \Drupal\user\Entity\User $account */
    $account = $event->getEntity();
    $mapping = $event->getMapping();
    $salesforceObj = $event->getMappedObject();

    // check if the mapping matches the one I want to revise
    switch ($mapping->id()) {
      case 'my_salesforce_mapping':
        if($account->isNew()) {
          // Be mindful that your salesforce fields may be different. Mine are "Email" and "Name"
          $sf_email = $event->getMappedObject()->getSalesforceRecord()->field('Email');
          if(empty($sf_email)) {
            $event->disallowPull(); // We will reject any salesforce records that don't have an email associated with it. You may want to do something different here
            break;
          }

          $drupal_user = user_load_by_mail($sf_email);

          // Find if a user with that email exists
          if($drupal_user != false) {
            $salesforceObj->setDrupalEntity($drupal_user);
            \Drupal::logger('salesforce')->debug("Update the user with email {$sf_email} instead of creating a duplicate");
          } else {
            \Drupal::logger('salesforce')->debug("There was an issue when updating the user - Unknown email for {$event->getMappedObject()->getSalesforceRecord()->field('Name')}");
          }
        } else {
          // I don't need to do anything because the entities are already mapped
        }
        break;
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events = [
      SalesforceEvents::PULL_PREPULL => 'pullPrepull',
    ];
    return $events;
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.