3

I would like to search the Civi API for Contacts that have both of two tags. I'm at a loss as to how to do this. Right now if you provide multiple tags, the default is to use an OR operation. For example:

CRM.api3('Contact', 'get', {
  "sequential": 1,
  "tag": [5,7]
  "return": ["tag","first_name"]
}).done(function(result) {
  // do something
});

This currently returns all contacts that have either tag 5 OR tag 7. I want to return all contacts that have both tag 5 AND tag 7. Is there any way to achieve this functionality?

  • Sorry, too lazy to open api explorer; just off the top of my head, shouldn't it be something like "tag": {"IN":5,7} ? – yurg Mar 4 '16 at 11:38
  • I tried "tag":{"IN":[5,7]}, but that just did the same thing; it's still an OR of the two tags – Brad Xanti Mar 4 '16 at 14:35
  • Than, may be, try Search Builder (someone has given me same advice somewhere in the past)? Mean find your contacts like this imgur.com/d9IPf3f (untested! but seems to be using AND, according to what it says) , add your findings in a "smart group" and than use API for querying that group? Quite complex path, but at least something.. – yurg Mar 4 '16 at 18:03
  • Thanks for helping me out. That doesn't seem like a feasible solution, because it's supposed to be dynamic, i.e. you're supposed to be able to select from several tags, not just two, and find the AND between all of them. I did look at the search builder; there's no way to use that in the AJAX API, correct? – Brad Xanti Mar 4 '16 at 18:51
  • Right, it doesn't seems like a solution at all, just a workaround for a very specific issue you've described initially. Just came to mind: you may want to try so called civicrm "chaining" requests, mean, request tag 1 and request tag 2 from tag 1 results. – yurg Mar 4 '16 at 19:09
1

I don't think you can do this with one api call.

Not optimal but you could do a postprocess in place of the // do something when the result comes.

Better, you could create your own api call in a custom extension - see https://wiki.civicrm.org/confluence/display/CRMDOC/Create+a+Module+Extension#CreateaModuleExtension-AddanAPIfunction

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.