Error: Call to a member function id() on null in /ebsvol/www/savant/web/modules/contrib/salesforce/modules/salesforce_mapping/src/Plugin/SalesforceMappingField/RelatedIDs.php on line 75

I am syncing (one-direction) from Drupal into SalesForce a number of custom ECK entities. One is what we call a "load" and it references both a "spec" (through field_spec) and a room (through field_room). Both room and spec entities are also synced into SalesForce.

During salesforce_push cron run, I started getting this shortly after going live. What I'm seeing is that $field->getValue() (line 67) works and returns an array of one with a target_id set, so it's not blank. However, in the line 75 sub-call it's trying to do $field->entity->id() and $field->entity is null. However, field->target_id works... if we only need the id, should we be calling target_id directly instead? Or, is there some other reason entity doesn't work? I'm not sure what the right thing is here.

Comments

cwells created an issue. See original summary.

aaronbauman’s picture

if $field->entity->id() doesn't work but $field->target_id does, is it possible that the entity was deleted but the reference did not get updated?

either way, it's probably fine to switch to $field->getValue() - this is preferable over $field->target_id, since different implementations my not use the same property. do you want to roll that patch?

chrisfromredfin’s picture

So actually digging, the first scenario is actually what happened. How the reference wasn't updated, I'm not sure - may be a bug in ECK or perhaps it not something that happens when entities are deleted with the REST API or something?

Would you accept another patch where I check for a null $field->entity and return also in that case? Something like...

    $field = $entity->get($field_name);
++    if (empty($field->getValue()) || is_null($field->entity)) {
++      // This reference field is blank or the referenced entity no longer exists.
      return;
    }

    // Now we can actually fetch the referenced entity.
    $field_settings = $field->getFieldDefinition()->getSettings();
    // @TODO this procedural call will go away when sf mapping object becomes a service or field
    $referenced_mappings = $this->mapped_object_storage->loadByDrupal($field_settings['target_type'], $field->entity->id());
aaronbauman’s picture

Yes, adding a type check is a good idea.

chrisfromredfin’s picture

Status: Active » Needs review
StatusFileSize
new740 bytes

K let's see where this lands!

  • aaronbauman committed e907c9b on 8.x-3.x authored by cwells
    Issue #3007055 by cwells: Call to a member function id() on null in...
aaronbauman’s picture

Status: Needs review » Fixed

Looks great - thanks for the patch

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.