I am using SalesForce suite to pull data from SalesForce to Drupal.
In the SalesForce object I have multiple date fields. These fields needs to map to a field-collection in the node. The field-collection can have multiple (unlimited) values.
So for example in the SalesForce object I have
sales_force_object -> start_date
sales_force_object -> event_date
sales_force_object -> end_date
which needs to map to
node -> the_dates [0] = sales_force_object -> start_date
node -> the_dates [1] = sales_force_object -> event_date
node -> the_dates [2] = sales_force_object -> end_date
The solution was to add 'temporary' fields on the node, let's call them:
node -> sync_start_date
node -> sync_event_date
node -> sync_end_date
When I get data from SalesForce (using pull; and yes the data comes through OK) - I map the values from the SalesForce object to the sync fields. Then just before the node is saved (updated), I use hook_node_presave
to take values from the sync fields and create the field-collection items. This is the code:
$fc = field_collection_item_load($key_dates_entity_id);
$fc_wrapper = entity_metadata_wrapper('field_collection_item', $fc);
$fc_wrapper->field_date->set((int) $date);
$fc_wrapper->save();
field_attach_update('node', $node);
field_attach_presave('node', $node);
entity_get_controller('node')->resetCache([$node->nid]);
You can safely assume that $key_dates_entity_id
is the correct field-collection entity ID.
Inspecting the code I can see the entity is correctly updated (nothing wrong with the data), and when I check the MySQL general-log, I can see the data being saved to the database, however, when the set of instructions end, the old (cached) data is also saved. In other words the DB is updated twice - once with the new value, and once with the old values: net result is field collection items are not updated.
I have tried all sorts of combinations - nothing works. Any help please.