1

Drupal 7.63 Civi 5.4

I have a Drupal webform for my contribution page and I want to allow donors that select "Is Recurring" the option to set the number of months for it to recur. So I have a Contribution custom data field "Recurring for X months" I am then using CiviRules to put that input into civicrm_contribution_recur > installments

I have a CiviRule set to trigger with Contribution is Added, I have no conditions set, and my Action is a custom action.

I have a watchdog built into my code so I can trouble shoot the code.

  protected function getApiEntity() {
    return 'ContributionRecur';
  }

  protected function getApiAction() {
    return 'Create';
  }

  protected function alterApiParameters($params, 
      CRM_Civirules_TriggerData_TriggerData $triggerData) {
    $contribution = $triggerData->getEntityData("Contribution");

    watchdog('CONTRIBUTION TRIGGER DATA', var_export($contribution, true));

    $params['id'] = $contribution['contribution_recur_id'];

    if ($contribution['custom_67'] > 1) {
        $params['installments'] = $contribution['custom_67'];
    }

    watchdog('CONTRIBUTION PARAMS', var_export($params, true));

    return $params;
  }

In the Drupal logs I am seeing the arrays from the watchdog. However, custom_67 value is empty (custom_67 is listed in the array). If I run a Contribution Get in the API Explorer, the array includes values for custom_67 for the contribution I just made. I am now testing with real live contributions rather than the test Payment Processor to be sure that everything is fully functioning.

I created a new CiviRule using "contribution is changed" and used the same above action. Then I used the API Explorer >Contribution > Create and changed the Amount & Recurring for X (custom_67) to a different # for a particular contribution ID. It worked, it put the custom_67 # into contribution_recur > installments. However, it put in the old number not the new number.

This lead me to think that maybe the CiviRules was triggering before the database finished writing to the custom table, so I added a 1 minute delay to the rule. I still get nothing returned in my watchdog arrays for values in custom_67.

Any ideas why my Action Trigger Data is not getting values for custom_67?

  • What could be the case is that the contribution is written tot the database but the contribution recur entity not yet. So maybe you could use a trigger contribution recur is added? – Jaap Jansma - CiviCooP Feb 20 at 7:16
  • Doesn't answer your question but I've had similar issues. The process is very dependent on the order of operations and for contributions, there can be many different things being updated. A higher-level logical hook would be useful so that you can say 'when Civi has finished doing its stuff for this set of contribution things, run my code' rather than needing to figure whether you need to be triggering from a contribution, recurring contribution, line item, custom value etc. – Aidan Feb 20 at 9:24
  • Actually ideally the hook that triggers the CiviCRM stuff should run off CRM_Core_Transaction::addCallback(CRM_Core_Transaction::PHASE_POST_COMMIT, <classname>', $callBackParams) rather than directly from the post hook – ErikH - CiviCooP Feb 20 at 13:20
  • Jaap Jansma, I did try triggering off the Recur added (just forgot to say that I tried that) but that is written first so that the ID is added contribution > contribution_recur_id I also looked to see if I could trigger off the custom table, etc. – Iowa Boy Feb 20 at 16:19
  • ErikH, I will start looking at using your trigger. – Iowa Boy Feb 20 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.