Answers.Body: <p>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.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-04T21:44:12.330Z
Answers.LastActivityDate: 2015-11-04T21:44:12.330Z
Answers.UserDisplayName: Jon G - Megaphone Tech
Body: <p>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.</p>
  
  <p>In Civi 4.4, this function worked fine.  In Civi 4.6, the soft credit is successfully created - and then immediately deleted!  The <code>$result</code> 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.</p>
  
  <p>After some experimentation, I found that if I try creating a soft credit to a DIFFERENT contribution, it works.  Confusing!</p>
  
  <p>What could be happening after <code>hook_civicrm_post</code> that would cause a soft credit attached to the contribution to be deleted? </p>
  
  <pre><code>function automaticsoftcredit_civicrm_post( $op, $objectName, $objectId, &amp;$objectRef ) {
    if($op == 'create' &amp;&amp; $objectName == 'Contribution'){
      $cid = $objectRef-&gt;contact_id;
  
      //Look up whether this person has a relationship_type_id that's automatically soft credited
      $params = array(
       'version' =&gt; 3,
        'sequential' =&gt; 1,
        'contact_id_a' =&gt; $cid,
        'relationship_type_id' =&gt; 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'] &gt; 0) {
        unset($params);
        foreach ($result['values'] as $relationship) {
          $params = array(
            'sequential' =&gt; 1,
            'contribution_id' =&gt; $objectId,
            'contact_id' =&gt; $relationship['contact_id_b'],
            'amount' =&gt; $objectRef-&gt;total_amount,
          );
          $result = civicrm_api3('ContributionSoft', 'create', $params);
  CRM_Core_Error::debug_var('result', $result);
  
        }
      }
  
    }
  </code></pre>
  
Tags: <api><hooks><developer>
Title: Soft credits created via API/hook_civicrm_post are automatically deleted in Civi 4.6
ThreadText: # Soft credits created via API/hook_civicrm_post are automatically deleted in Civi 4.6
  
  (Asked by: username_0 on 2015-11-04)
  
  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);
  
        }
      }
  
    }
  
  ```
  
  **Tags:** api, hooks, developer
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2015-11-04. Score: 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.
  
  
  ---
  
  