3

Let's say I have a contact which exists and has id = 12345. I can use the API like this...

CRM.api3('Contact', 'create', {"id": 12345, "nick_name": "Bob"});

to change the contact's nick name. Cool.


But when I try this...

CRM.api3('Contact', 'create', {"id": 12345, "nick_name": null});

the call completes with success but does not set the nick_name field to NULL as I'd expect. Why? Is there some other way I can do it?

3

It doesn't quite feel right to me on the empty vs null front - playing around with the API explorer in 4.7.15, but what I can say is if I do an api call for a contact ID (say, 3) with nickname IS NULL as follows:

CRM.api3('Contact', 'get', {
  "sequential": 1,
  "id": 3,
  "nick_name": {"IS NULL":1}
})

I come up with the result for cid 3 including "nick_name": "", (which would say to me that it's empty instead of IS NULL... but comes up with the previous api call anyways.

Then I run the following test by adding a nickname:

CRM.api3('Contact', 'create', {
  "sequential": 1,
  "contact_type": "Individual",
  "id": 3,
  "nick_name": "nickname"
})

and run the same GET query as above, and get no results as follows:

{
    "is_error": 0,
    "version": 3,
    "count": 0,
    "values": []
}

If I then run:

CRM.api3('Contact', 'create', {
  "sequential": 1,
  "contact_type": "Individual",
  "id": 3,
  "nick_name": ""
})

and run the first GET query... I come up with the same result as the beginning again... which appears to be a contact with nickname empty ("") rather than null.

TLDR; It looks like you can use

CRM.api3('Contact', 'create', {
  "sequential": 1,
  "contact_type": "Individual",
  "id": 3,
  "nick_name": ""
})

even though it doesn't feel quite right... if you're trying to pull that contact up with "nickname IS NULL" searches using the api.

  • 1
    This is cool. To my surprise, supplying an empty string for the field value, actually does set the field to NULL, and it works for other non-string field types like int and datetime. – Sean Mar 28 '17 at 20:09
  • Sounds like it's a feature! Thanks for confirming :) – Eli Lisseck Mar 28 '17 at 20: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.