Answers.Body: <p>I never figured out what was causing this problem, but the solution seems to have been upgrading from 5.55 to 5.66.  I did the upgrade all at once, so I'm not sure which specific version resolved it.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-01-29T05:00:26.280Z
Answers.LastActivityDate: 2024-01-29T05:00:26.280Z
Answers.UserDisplayName: Ken Williams
Body: <p>I recently migrated a CiviCRM database from Drupal to Wordpress, but using the same version of CiviCRM (5.55.2).  Now when I try to export contacts to CSV using the web UI, I'm getting this error:</p>
  <pre><code>Fatal error: Uncaught CRM_Core_Exception: [0: Invalid postal greeting thrown in
   *redacted*/www/wp/wp-content/plugins/civicrm/civicrm/api/api.php on line 135
  </code></pre>
  <p>That exception is being thrown here, in <code>civicrm_api3</code>:</p>
  <pre><code>function civicrm_api3(string $entity, string $action, array $params = []) {
    $params['version'] = 3;
    $result = \Civi::service('civi_api_kernel')-&gt;runSafe($entity, $action, $params);
    if (is_array($result) &amp;&amp; !empty($result['is_error'])) {
      throw new CRM_Core_Exception($result['error_message'], CRM_Utils_Array::value('error_code', $result, 'undefined'), $result);
    }
    return $result;
  }
  </code></pre>
  <p>The error happens even before I can select fields for export, so it must be happening when constructing the result object?</p>
  <p>Any suggestions for fixing this?</p>
  <p>I do plan to upgrade to a newer CiviCRM, but I'm not sure whether I should fix this problem first (bad data?) or attempt upgrades even though this is happening.</p>
  <p><strong>Edit to add</strong>: there's no error being written to <code>wp-content/uploads/civicrm/ConfigAndLog</code> when I get this error in the browser.  HOWEVER, there's an error there every once in a while, which I think is unrelated but since it's the only problem appearing in the logs I'll include it here:</p>
  <pre><code>  &quot;message&quot; =&gt; &quot;The requested Profile (gid=12) is disabled OR it is not configured 
  to be used for 'Profile' listings in its Settings OR there is no Profile with 
  that ID OR you do not have permission to access this profile. Please contact 
  the site administrator if you need assistance.&quot;
  </code></pre>
  <p><strong>Edit again:</strong> It looks like there are actually quite a few more errors that I hadn't noticed before, such as these:</p>
  <pre><code>Warning: Undefined array key &quot;groups&quot; in *redacted*/wp-content/uploads/civicrm/templates_c/en_US/%%1B/1BD/1BD7DE8F%%Statistics.tpl.php on line 14
  
  Warning: Undefined array key 4182 in *redacted*/wp-content/uploads/civicrm/templates_c/en_US/%%33/33E/33E84618%%Detail.tpl.php on line 185
  
  Warning: Trying to access array offset on value of type null in *redacted*/wp-content/uploads/civicrm/templates_c/en_US/%%33/33E/33E84618%%Detail.tpl.php on line 185
  
  Warning: Undefined array key 4182 in *redacted*/wp-content/uploads/civicrm/templates_c/en_US/%%33/33E/33E84618%%Detail.tpl.php on line 185
  
  Warning: Trying to access array offset on value of type null in *redacted*/wp-content/uploads/civicrm/templates_c/en_US/%%33/33E/33E84618%%Detail.tpl.php on line 185
  
  Warning: Undefined array key 4182 in *redacted*/wp-content/uploads/civicrm/templates_c/en_US/%%33/33E/33E84618%%Detail.tpl.php on line 185
  
  Warning: Trying to access array offset on value of type null in *redacted*/wp-content/uploads/civicrm/templates_c/en_US/%%33/33E/33E84618%%Detail.tpl.php on line 185
  </code></pre>
  <p>Does it look like there's some version software mismatch somewhere?  I've cleared out the templates cache but they get recreated and still generate this same error.</p>
  <p><strong>Edit the third:</strong></p>
  <p>I added some debugging info to the thrown exception:</p>
  <pre><code>      if (!in_array($greetingVal, $greetings)) {
          throw new CRM_Core_Exception(ts('Invalid %1 greeting %2 (type %4) in %3',
            [1 =&gt; $key, 2 =&gt; $greetingVal, 3 =&gt; var_export($greetings, true), 4 =&gt; gettype($greetingVal)]));
        }
  </code></pre>
  <p>Output:</p>
  <pre><code>Fatal error: Uncaught CRM_Core_Exception: [0:
   Invalid postal greeting 1 (type string) in array (
   1 =&gt; 'Dear {contact.first_name}',
   2 =&gt; 'Dear {contact.prefix_id:label} {contact.first_name} {contact.last_name}',
   3 =&gt; 'Dear {contact.prefix_id:label} {contact.last_name}',
   4 =&gt; 'Customized',
   5 =&gt; 'Dear {contact.household_name}', ) thrown
  </code></pre>
  <p>So it looks like there's a type mismatch, string vs. int - not sure how that happens since <code>civicrm_contact.postal_greeting_id</code> is declared in the database as <code>int(10) unsigned</code>.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-12-18T03:08:14.530Z
Comments.Text: Try adding in some debug code at https://github.com/civicrm/civicrm-core/blob/0285562c912db64e51a15236820f51ff7c9d4557/api/v3/Contact.php#L649 which is where the error is actually coming from to tell you what the variable values are and that might give some clues.
Comments.UserDisplayName: Demerit
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-12-19T00:23:11.837Z
Comments.Text: Thanks @Demerit, I'll do that.  I've also added some additional errors I seem to be getting.
Comments.UserDisplayName: Ken Williams
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-12-19T01:39:31.417Z
Comments.Text: Yep. There's a constant stream of errors like that in lots of places. But these particular ones don't seem relevant to the greeting problem, more likely from viewing a civireport.
Comments.UserDisplayName: Demerit
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-12-20T03:02:14.113Z
Comments.Text: @Demerit I added some debugging info in the exception, it looks like there's a type mismatch `int` vs. `string` here, but I'm not sure where that would come from.
Comments.UserDisplayName: Ken Williams
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-12-21T06:42:33.717Z
Comments.Text: Ok I think then the next step to find where it comes from would be just before the throw, put `CRM_Core_Error::backtrace('greeting', TRUE);`, which will add a backtrace into the civi log. You may also want to log `var_export($params, true)` which might point to a specific contact being the problem.
Comments.UserDisplayName: Demerit
Tags: <error>
Title: Uncaught CRM_Core_Exception: [0: Invalid postal greeting
ThreadText: # Uncaught CRM_Core_Exception: [0: Invalid postal greeting
  
  (Asked by: username_0 on 2023-12-18)
  
  I recently migrated a CiviCRM database from Drupal to Wordpress, but using the same version of CiviCRM (5.55.2). Now when I try to export contacts to CSV using the web UI, I'm getting this error:
  
  ```
  Fatal error: Uncaught CRM_Core_Exception: [0: Invalid postal greeting thrown in
   *redacted*/www/wp/wp-content/plugins/civicrm/civicrm/api/api.php on line 135
  
  ```
  
  That exception is being thrown here, in `civicrm_api3`:
  
  ```
  function civicrm_api3(string $entity, string $action, array $params = []) {
    $params['version'] = 3;
    $result = \Civi::service('civi_api_kernel')->runSafe($entity, $action, $params);
    if (is_array($result) && !empty($result['is_error'])) {
      throw new CRM_Core_Exception($result['error_message'], CRM_Utils_Array::value('error_code', $result, 'undefined'), $result);
    }
    return $result;
  }
  
  ```
  
  The error happens even before I can select fields for export, so it must be happening when constructing the result object?
  
  Any suggestions for fixing this?
  
  I do plan to upgrade to a newer CiviCRM, but I'm not sure whether I should fix this problem first (bad data?) or attempt upgrades even though this is happening.
  
  **Edit to add**: there's no error being written to `wp-content/uploads/civicrm/ConfigAndLog` when I get this error in the browser. HOWEVER, there's an error there every once in a while, which I think is unrelated but since it's the only problem appearing in the logs I'll include it here:
  
  ```
    "message" => "The requested Profile (gid=12) is disabled OR it is not configured 
  to be used for 'Profile' listings in its Settings OR there is no Profile with 
  that ID OR you do not have permission to access this profile. Please contact 
  the site administrator if you need assistance."
  
  ```
  
  **Edit again:** It looks like there are actually quite a few more errors that I hadn't noticed before, such as these:
  
  ```
  Warning: Undefined array key "groups" in *redacted*/wp-content/uploads/civicrm/templates_c/en_US/%%1B/1BD/1BD7DE8F%%Statistics.tpl.php on line 14
  
  Warning: Undefined array key 4182 in *redacted*/wp-content/uploads/civicrm/templates_c/en_US/%%33/33E/33E84618%%Detail.tpl.php on line 185
  
  Warning: Trying to access array offset on value of type null in *redacted*/wp-content/uploads/civicrm/templates_c/en_US/%%33/33E/33E84618%%Detail.tpl.php on line 185
  
  Warning: Undefined array key 4182 in *redacted*/wp-content/uploads/civicrm/templates_c/en_US/%%33/33E/33E84618%%Detail.tpl.php on line 185
  
  Warning: Trying to access array offset on value of type null in *redacted*/wp-content/uploads/civicrm/templates_c/en_US/%%33/33E/33E84618%%Detail.tpl.php on line 185
  
  Warning: Undefined array key 4182 in *redacted*/wp-content/uploads/civicrm/templates_c/en_US/%%33/33E/33E84618%%Detail.tpl.php on line 185
  
  Warning: Trying to access array offset on value of type null in *redacted*/wp-content/uploads/civicrm/templates_c/en_US/%%33/33E/33E84618%%Detail.tpl.php on line 185
  
  ```
  
  Does it look like there's some version software mismatch somewhere? I've cleared out the templates cache but they get recreated and still generate this same error.
  
  **Edit the third:**
  
  I added some debugging info to the thrown exception:
  
  ```
        if (!in_array($greetingVal, $greetings)) {
          throw new CRM_Core_Exception(ts('Invalid %1 greeting %2 (type %4) in %3',
            [1 => $key, 2 => $greetingVal, 3 => var_export($greetings, true), 4 => gettype($greetingVal)]));
        }
  
  ```
  
  Output:
  
  ```
  Fatal error: Uncaught CRM_Core_Exception: [0:
   Invalid postal greeting 1 (type string) in array (
   1 => 'Dear {contact.first_name}',
   2 => 'Dear {contact.prefix_id:label} {contact.first_name} {contact.last_name}',
   3 => 'Dear {contact.prefix_id:label} {contact.last_name}',
   4 => 'Customized',
   5 => 'Dear {contact.household_name}', ) thrown
  
  ```
  
  So it looks like there's a type mismatch, string vs. int - not sure how that happens since `civicrm_contact.postal_greeting_id` is declared in the database as `int(10) unsigned`.
  
  **Tags:** error
  
  > **Comments:**
  > **username_1**: Try adding in some debug code at https://github.com/civicrm/civicrm-core/blob/0285562c912db64e51a15236820f51ff7c9d4557/api/v3/Contact.php#L649 which is where the error is actually coming from to tell you what the variable values are and that might give some clues.  
  > **username_0**: Thanks @username_1, I'll do that.  I've also added some additional errors I seem to be getting.  
  > **username_1**: Yep. There's a constant stream of errors like that in lots of places. But these particular ones don't seem relevant to the greeting problem, more likely from viewing a civireport.  
  > **username_0**: @username_1 I added some debugging info in the exception, it looks like there's a type mismatch `int` vs. `string` here, but I'm not sure where that would come from.  
  > **username_1**: Ok I think then the next step to find where it comes from would be just before the throw, put `CRM_Core_Error::backtrace('greeting', TRUE);`, which will add a backtrace into the civi log. You may also want to log `var_export($params, true)` which might point to a specific contact being the problem.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2024-01-29. Score: 1 | Accepted answer
  
  I never figured out what was causing this problem, but the solution seems to have been upgrading from 5.55 to 5.66. I did the upgrade all at once, so I'm not sure which specific version resolved it.
  
  
  ---
  
  