Answers.Body: <p>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 <code>&amp;$params</code> variable is passed by reference so you can modify it before the contact is written to the database.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-10-29T19:13:06.920Z
Answers.Text: Thanks a mil! I lost a plot for Contacts $params while digging into Relationships $params and API paths.
Answers.UserDisplayName: yurg
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-10-30T08:00:26.537Z
Answers.Text: You don't need to use the API at all here, but in situations where you do then using APIv4 is much preferred now.
Answers.UserDisplayName: Aidan
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-10-28T22:21:51.073Z
Answers.LastActivityDate: 2023-10-28T22:21:51.073Z
Answers.UserDisplayName: Coleman
Body: <p>Getting a loop and consequently out of memory while executing the code:</p>
  <pre><code>  function hook_civicrm_pre($op, $objectName, $id, &amp;$params){
        if ('Individual' === $objectName &amp;&amp; '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' =&gt; 'Individual',
                      'id' =&gt; $id,
                      'custom_999' =&gt; $condition,
                    ]);
     CRM_Core_Error::debug_var('Line '.__LINE__, print_r($result, true));
    }
   }
  </code></pre>
  <p>Have tried also</p>
  <pre><code>foreach ($params as $key =&gt; $value) {
    if (0 === strpos($key, 'custom_999')) {
      $params[$key] = $condition;
  }
  </code></pre>
  <p>}</p>
  <p>Which doesn't seem to pick up the latest changes, eg., works only 2nd saving. Can't see what's wrong here.</p>
  
Tags: <api><hooks>
Title: civicrm_api3('Contact', 'create', in hook_civicrm_pre?
ThreadText: # civicrm_api3('Contact', 'create', in hook_civicrm_pre?
  
  (Asked by: username_0 on 2023-10-27)
  
  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.
  
  **Tags:** api, hooks
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-10-28. Score: 3 | Accepted answer
  
  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.
  
  > **Comments:**
  > **username_0**: Thanks a mil! I lost a plot for Contacts $params while digging into Relationships $params and API paths.  
  > **username_2**: You don't need to use the API at all here, but in situations where you do then using APIv4 is much preferred now.  
  
  ---
  
  