1

I am looking to get a list of all contacts with active memberships, and their respective tags. I went about this by starting from an api call to give me all current memberships owned by a company.

CRM.api3('Membership', 'get', {
  "sequential": 1,
  "return": ["contact_id.tag","contact_id.id"],
  "status_id": "Current",
  "contact_id.contact_type": "Organization"
}).done(function(result) {
  // do something
});

Running the above only gets me the default returns vals of the contact_id and id of the membership. It does not return the tags.

When I run this by itself:

$result = civicrm_api3('EntityTag', 'getsingle', array(
  'sequential' => 1,
  'contact_id' => 137402,
));

I get what I want, the tag ids for the contact.

What am I doing wrong here?

2 Answers 2

1

Ideally I would be using the $value.contact_id var but I can't get it to work that way.

membership->tags (via contact_id)

What I did get working was inserting a 3rd link in the api call chain.

membership->contact->tags

CRM.api3('Membership', 'get', {
  "sequential": 1,
  "status_id": "Current",
  "options": {"limit":0},
  "contact_id.contact_type": "Organization",
  "api.Contact.get": {"api.EntityTag.get":{}}
}).done(function(result) {
  // do something
});
1
  • A little late but just perused this... might be obvious to others but in case it's helpful... to get $value.contact_id working I had to escape the $. So my chained call looked like 'api.Contact.get' => array('id' => "\$value.contact_id", etc Sep 1, 2016 at 17:23
1

First, you need to do chained api call, like you did with :

"api.Contact.get": { "sequential": 1, "api.EntityTag.get":{}}

Second, you can filter the result of your chained call by doing so :

"$value.api.Contact.get.contact_type": "Organization"

Third, to get the result of your chained api call, you need to do something like :

tags = result['values'][0]['api.Contact.get']['values'][0]['api.EntityTag.get']['values'];

If you want something a little less complex, you can use getsingle or getvalue instead of get. This way, you won't have to loop on values

So the final result should look like :

CRM.api3('Membership', 'get', {
  "sequential": 1,
  "api.Contact.getsingle": {
    "api.EntityTag.get" : {}
  }
  "status_id": "Current",
  "$value.api.Contact.getsingle.contact_type": "Organization"  // instead of contact_id.contact_type
}).done(function(result) {
  // replace by a loop on membership...
  tags = result['values'][0]['api.Contact.getsingle']['api.EntityTag.get']['values'];
});

Your Answer

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

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