Answers.Body: <p>The answer depends on how you call the API: is this from php or via the ajax or rest interface?</p>
  
  <p>in php, both null and empty string are seen as a boolean false, so</p>
  
  <pre><code>if (!$custom_42)
     echo "it's either null or empty string" 
  </code></pre>
  
  <p>As for why custom fields behave this way: each custom field belong to a custom group that is stored in a table. if you don't have any of the custom fields in that group, the row isn't created, so all the fields are null, but as soon as you set one of the fields, it creates a row, and the default value of each of the other empty fields depends of their type.</p>
  
  <p>That custom fields that are string become "" instead of null is arguable and it would seem more coherent they stay the same, but as both are seen as false in php (and other languages have the same conversion rules) probably not a big issue.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-27T16:28:23.670Z
Answers.Text: The problem appears to be caused by a CRM_Utils_System::isNull() check.
  
  This means thay any API parameters which specify checking for empty strings are ignored completely.
  
  Line1538 of CRMContact/BAO/Query.php
Answers.UserDisplayName: RocXa
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-20T04:16:57.887Z
Answers.LastActivityDate: 2015-05-20T04:25:09.123Z
Answers.LastEditDate: 2015-05-20T04:25:09.123Z
Answers.UserDisplayName: Xavier
Body: <p>Is there a way to check if a <strong>custom field</strong> is empty using the API?</p>
  
  <p>The check for 'IS Not Null' appears to work to an extent, but only if a custom field is not part of a set of custom fields, When one of the custom fields get given a value, the others change from Nulls to blanks.</p>
  
  <pre><code>e.g.
  custom_1 = Null
  custom_2 = Null
  custom_3 = Null
  
  set custom_1 = "some_string"
  custom_2 = (becomes empty string)
  custom_3 = (becomes empty string)
  </code></pre>
  
  <p>API is null check fails as they are no longer null..</p>
  
  <pre><code>API check for != '' doesn't work
  API check for &lt;&gt; '' doesn't work
  etc etc
  </code></pre>
  
  <p>Any thoughts or is this a bug?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-05-19T21:00:23.193Z
Comments.Text: Do you mean that even though the field is an empty string, the api returns an incorrect result if you check to see if it's an empty string?
Comments.UserDisplayName: Coleman
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-05-19T23:44:17.387Z
Comments.Text: Is the API related to this, or are the records perhaps saved to the database as empty strings when one of the fields in the set is given a value?
Comments.UserDisplayName: Nicholai
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-05-20T21:27:21.180Z
Comments.Text: This is using the API to filter out contacts that don't have certain custom fields populated.. 
  
  Trying >
  'custom_582' => array('>' => "")   finds blanks
  
  Trying "=
  'custom_582' => array('!=' => "")   finds blanks
Comments.UserDisplayName: RocXa
Tags: <api><custom-fields>
Title: CiviCRM API and checking if a custom field is empty rather than not null
ThreadText: # CiviCRM API and checking if a custom field is empty rather than not null
  
  (Asked by: username_0 on 2015-05-19)
  
  Is there a way to check if a **custom field** is empty using the API?
  
  The check for 'IS Not Null' appears to work to an extent, but only if a custom field is not part of a set of custom fields, When one of the custom fields get given a value, the others change from Nulls to blanks.
  
  ```
  e.g.
  custom_1 = Null
  custom_2 = Null
  custom_3 = Null
  
  set custom_1 = "some_string"
  custom_2 = (becomes empty string)
  custom_3 = (becomes empty string)
  
  ```
  
  API is null check fails as they are no longer null..
  
  ```
  API check for != '' doesn't work
  API check for <> '' doesn't work
  etc etc
  
  ```
  
  Any thoughts or is this a bug?
  
  **Tags:** api, custom-fields
  
  > **Comments:**
  > **username_1**: Do you mean that even though the field is an empty string, the api returns an incorrect result if you check to see if it's an empty string?  
  > **username_2**: Is the API related to this, or are the records perhaps saved to the database as empty strings when one of the fields in the set is given a value?  
  > **username_0**: This is using the API to filter out contacts that don't have certain custom fields populated..   Trying > 'custom_582' => array('>' => "")   finds blanks  Trying "= 'custom_582' => array('!=' => "")   finds blanks  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_3 on 2015-05-20. Score: 2
  
  The answer depends on how you call the API: is this from php or via the ajax or rest interface?
  
  in php, both null and empty string are seen as a boolean false, so
  
  ```
  if (!$custom_42)
     echo "it's either null or empty string" 
  
  ```
  
  As for why custom fields behave this way: each custom field belong to a custom group that is stored in a table. if you don't have any of the custom fields in that group, the row isn't created, so all the fields are null, but as soon as you set one of the fields, it creates a row, and the default value of each of the other empty fields depends of their type.
  
  That custom fields that are string become "" instead of null is arguable and it would seem more coherent they stay the same, but as both are seen as false in php (and other languages have the same conversion rules) probably not a big issue.
  
  > **Comments:**
  > **username_0**: The problem appears to be caused by a CRM_Utils_System::isNull() check.  This means thay any API parameters which specify checking for empty strings are ignored completely.  Line1538 of CRMContact/BAO/Query.php  
  
  ---
  
  