2

I have a custom extension "Automatic Soft Credit", which replicates a function from Raiser's Edge - if contact A has the relationship "Automatically Soft Credits" to contact B, then any contributions for contact A will automatically be soft credited to contact B.

In Civi 4.4, this function worked fine. In Civi 4.6, the soft credit is successfully created - and then immediately deleted! The $result below returns success, but the soft credit isn't in the database - but the auto-increment happens, so it seems to be created then deleted.

After some experimentation, I found that if I try creating a soft credit to a DIFFERENT contribution, it works. Confusing!

What could be happening after hook_civicrm_post that would cause a soft credit attached to the contribution to be deleted?

function automaticsoftcredit_civicrm_post( $op, $objectName, $objectId, &$objectRef ) {
  if($op == 'create' && $objectName == 'Contribution'){
    $cid = $objectRef->contact_id;

    //Look up whether this person has a relationship_type_id that's automatically soft credited
    $params = array(
     'version' => 3,
      'sequential' => 1,
      'contact_id_a' => $cid,
      'relationship_type_id' => 11, //FIXME: This relationship_type_id is currently hardcoded, we should load it from settings
    );
    $result = civicrm_api('Relationship', 'get', $params);
    //if we have the auto soft credit relationship for one or more contacts, create a soft credit for each
    if($result['count'] > 0) {
      unset($params);
      foreach ($result['values'] as $relationship) {
        $params = array(
          'sequential' => 1,
          'contribution_id' => $objectId,
          'contact_id' => $relationship['contact_id_b'],
          'amount' => $objectRef->total_amount,
        );
        $result = civicrm_api3('ContributionSoft', 'create', $params);
CRM_Core_Error::debug_var('result', $result);

      }
    }

  }

1 Answer 1

1

This is to do with code introduced in CRM-16804, and is specific to soft credits. It's not a generic API issue as I'd feared. I'll update this answer with a more complete solution once I've nailed it down.

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.