4

I'm working with the CiviCRM API and it's been great--I've been able to make a very useful tool for our team. However, I've encountered a problem.

I would like to do a search on contacts based on a tag. However, whenever I make a call to the database asking for a particular tag, I get an error. The following is an example I used on the demo site:

CRM.api3('Contact', 'get', {
  "sequential": 1,
  "tag": "Company"
}).done(function(result) {
  // do something
});

I receive the following error in the response:

{
  "error_code": "no such field",
  "tip": "add debug=1 to your API call to have more info about the error",
  "is_error": 1,
  "error_message": "DB Error: no such field"
}

Now, I receive a different error in test use on my own site, but is it possible in some way to do a search in the API for a contact based on tag?

1 Answer 1

6

you are almost there: instead of using the tag name, use the tag id (eg 5 for Volunteers)

CRM.api3('Contact', 'get', {
  "debug": 1,
  "sequential": 1,
  "return": ["tag"],
  "tag": "5"
}).done(function(result) {
  console.log(result);
});

And for performance reasons, we recommend to always set the return param with the list of fields you actually need (by default, it returns way too many that you don't need)

Otherwise, yes, it should work with the name of the tag instead of the id, but given that tags are one of the few entities that don't have an internal name that only the dev can change, I'd rather rely on the id rather than on the visible name that your users can alter

3
  • Thanks! This is super helpful. I was afraid we would have to go through and add a new custom field and search by that. Is there any extensive documentation of the API? Something that might contain a little more in-depth info like this? What exists on the Civi site right now is pretty sparse. Thanks again.
    – Brad Xanti
    Commented Feb 22, 2016 at 15:50
  • In addition, do you know of a way to do an AND on the tags? For example, if I wanted to find any contact that had tag: 5 and tag: 7, is there any way to do that with just 1 Ajax request? It seems to me that it defaults to OR functionality. It returns all contacts with either tag: 5 or tag: 7.
    – Brad Xanti
    Commented Feb 22, 2016 at 21:24
  • 1
    hi, the api explorer and all the generated examples from the unit tests are pretty extensive. as for new questions, could you create them as new questions, the comment format is aweful as soon as it's more than a line or two ;)
    – Xavier
    Commented Feb 23, 2016 at 8:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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