0

Getting a loop and consequently out of memory while executing the code:

  function hook_civicrm_pre($op, $objectName, $id, &$params){
      if ('Individual' === $objectName && 'edit' === $op) {
     $myconditions = [];
     // make a few API calls checking Relationships, custom field balues, ets, gathering the data
     // print_r($myconditions) = $myconditions[TRUE, TRUE, TRUE];
   
   $condition = !in_array(false, $myconditions) ? 1 : 0;       
     
     // An individual custom field value update
       $result = civicrm_api3('Contact', 'create', [
                    'contact_type' => 'Individual',
                    'id' => $id,
                    'custom_999' => $condition,
                  ]);
   CRM_Core_Error::debug_var('Line '.__LINE__, print_r($result, true));
  }
 }

Have tried also

foreach ($params as $key => $value) {
  if (0 === strpos($key, 'custom_999')) {
    $params[$key] = $condition;
}

}

Which doesn't seem to pick up the latest changes, eg., works only 2nd saving. Can't see what's wrong here.

1 Answer 1

3

Running a contact update from within a contact update will cause an infinite loop. Instead of firing a new update action, just adjust the parameters of the action that's already happening. The &$params variable is passed by reference so you can modify it before the contact is written to the database.

2
  • Thanks a mil! I lost a plot for Contacts $params while digging into Relationships $params and API paths.
    – yurg
    Oct 29, 2023 at 19:13
  • You don't need to use the API at all here, but in situations where you do then using APIv4 is much preferred now.
    – Aidan
    Oct 30, 2023 at 8:00

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.