You are here

function salesforce_api_fieldmap_export_create in Salesforce Suite 7

Same name and namespace in other branches
  1. 5.2 salesforce_api/salesforce_api.module \salesforce_api_fieldmap_export_create()
  2. 6.2 salesforce_api/salesforce_api.module \salesforce_api_fieldmap_export_create()
  3. 7.2 salesforce_api/salesforce_api.module \salesforce_api_fieldmap_export_create()

Creates an object for export to Salesforce based on the supplied Drupal object and fieldmap.

Parameters

$fieldmap: The index of the fieldmap used to filter the Drupal object into the export.

$drupal_data: The Drupal object used to generate the export.

Return value

An object containing data ready for export to Salesforce or FALSE if the operation failed.

3 calls to salesforce_api_fieldmap_export_create()
salesforce_api_demo in salesforce_api/salesforce_api.admin.inc
Demonstrates some of the API functionality through the Salesforce class and fieldmap functionality.
sf_entity_export in sf_entity/sf_entity.module
Exports an entity to Salesforce using the specified fieldmap and stores the ID of the Salesforce object for the entity.
sf_entity_salesforce_form in sf_entity/sf_entity.module

File

salesforce_api/salesforce_api.module, line 675
Defines an API that enables modules to interact with the Salesforce server.

Code

function salesforce_api_fieldmap_export_create($fieldmap, $drupal_data = NULL) {

  // Load the fieldmap from the database.
  $map = salesforce_api_fieldmap_load($fieldmap);

  // Fail if the fieldmap does not exist.
  if (!$map) {
    return FALSE;
  }
  $drupal_object_definition = salesforce_api_fieldmap_objects_load('drupal', $map['drupal_entity'], $map['drupal_bundle']);
  $sf_object_definition = salesforce_api_fieldmap_objects_load('salesforce', 'salesforce', $map['salesforce']);
  $object = new stdClass();

  // Loop through the fields on the fieldmap.
  foreach ($map['fields'] as $sf_fieldname => $drupal_fieldname) {
    $sf_field_definition = $sf_object_definition['fields'][$sf_fieldname];
    $updateable = $sf_field_definition['type'] & SALESFORCE_FIELD_UPDATEABLE;
    $createable = $sf_field_definition['type'] & SALESFORCE_FIELD_CREATEABLE;

    // Don't try to update or create fields to which those actions do not apply.
    if (!$updateable && !$createable || empty($drupal_data->salesforce['sfid']) && !$createable || !empty($drupal_data->salesforce['sfid']) && !$updateable) {
      continue;
    }

    // If a handler is specified for retrieving a value for the Drupal field...
    if (isset($drupal_object_definition['fields'][$drupal_fieldname]['export'])) {
      $drupal_field_export_handler = $drupal_object_definition['fields'][$drupal_fieldname]['export'];
      $drupal_field_definition = $drupal_object_definition['fields'][$drupal_fieldname];

      // Get the value for the field from the handler function.
      $object->{$sf_fieldname} = htmlentities($drupal_field_export_handler($drupal_data, $drupal_fieldname, $drupal_field_definition, $sf_field_definition));
    }
    elseif (isset($drupal_data->{$drupal_fieldname})) {
      $object->{$sf_fieldname} = htmlentities($drupal_data->{$drupal_fieldname});
    }
  }

  // Before we return the object, we need to check for any fieldsToNull.
  // Leaving a field blank will not erase an existing value from Salesforce.
  // Any such value must be explicitly set to NULL.
  // TODO: Fields should be checked for nillable before getting added to fieldsToNull
  $props = get_object_vars($object);
  foreach ($props as $key => $value) {
    $sf_field_definition = $sf_object_definition['fields'][$sf_fieldname];

    // Salesforce treats the following values differently than NULL.
    if ($value === FALSE || $value === 0 || $value === '0' || $value === 'FALSE') {
      $object->{$key} = $value = 0;
    }
    elseif (empty($value)) {
      $nillable = $sf_field_definition['type'] & SALESFORCE_FIELD_NILLABLE;

      // If the field is not nillable, don't try to set it to NULL
      if (!$nillable) {
        switch ($sf_field_definition['salesforce']['type']) {
          case 'boolean':
            $object->{$key} = 0;
            break;
          case 'string':
            $object->{$key} = t('(blank)');
            break;
          default:
            unset($object->{$key});
            break;
        }
      }
      else {
        $object->fieldsToNull = $key;

        // Enterprise client can only NULL one field per transaction.
        // This is a bug beyond our control. For now, we just have to deal with it.
        // The following doesn't actually work:
        // if (!empty($object->fieldsToNull)) {
        //   $object->fieldsToNull .= '; ';
        // }
      }
    }
  }
  return $object;
}