4

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?

  • 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? – Coleman May 19 '15 at 21:00
  • 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? – Nicholai May 19 '15 at 23:44
  • 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 – RocXa May 20 '15 at 21:27
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.

  • 1
    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 – RocXa May 27 '15 at 16:28

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.