2

I'm using the Salesforce Suite module.

What I'm trying to accomplish:

When a record is created in Salesforce, that record is pulled into Drupal and the new node content ID is pushed back to the Salesforce record (for reference in Salesforce).

I have a mapping set up as such:

Triggers

  • Drupal create (push)
  • Drupal update (push)
  • Salesforce create (pull)
  • Salesforce update (pull)

Fields

  • Title - Salesforce to Drupal
  • Content ID - Drupal to Salesforce

With this setup, it pulls the record into Drupal but the Content ID doesn't get pushed back to Salesforce. It will, however push when I update (just save) the node but we don't expect these nodes to be saved anytime soon after it's pulled in.

I've tried a few different ways leveraging the events dispatched by the Salesforce module but it always leads me to a race condition or a circle.

  • Isn't there some setting in the module on Drupal that lets you expose a certain URL you can call via cron to have the sync triggered? Do that. Another alternative would probably be to trigger resaving nodes on cron. Or maybe there's some hook that can be triggered right after the node got stored to then resave it to trigger the sync. Can you have a look around, maybe hook_entity_insert or similar? – leymannx Nov 21 '19 at 8:24
  • If I enable the standalone url, it disables core cron processing for that mapping, forcing me to leverage that url for everything... not ideal. I can save the node on cron run but this leaves me having to wait until next cron for the node ID to be pushed to salesforce. I'm looking for something simultaneous. – drupalphil Dec 20 '19 at 16:02
  • I took a second glance at your advice. See answer below. Thanks @leymannx – drupalphil Dec 20 '19 at 20:10
2

Here's how I did it...

Since content is set to push (immediately) on update, I added a hook_entity_insert() that responded not to the content insert but the "salesforce_mapped_object" entity.

This ensures that not only was the node/user created but a mapping was created.

/**
 * Implements hook_entity_insert().
 */
function MYMODULE_entity_insert(Drupal\Core\Entity\EntityInterface $entity) {
  if ($entity->bundle() == 'salesforce_mapped_object') {
    $entity_info = $entity->get('drupal_entity')->first()->getValue();
    $type = $entity_info['target_type'];
    $id = $entity_info['target_id'];
    switch ($type) {
      case 'node':
        $node = \Drupal\node\Entity\Node::load($id);
        $node->save();
        break;
      case 'user':
        $user = \Drupal\user\Entity\User::load($id);
        $user->save();
        break;
    }
  }
}

Thanks to @leymannx in the above comment for the guidance.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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