2

Wilma has relationship: is campaigner at organisation System Change Not Charity.

How can I get a list of individuals like Wilma and the organisations that they are campaigners for?

I've read the documentation but I can't figure out how I would join on relationship?

I tried this which I thought would at least give me the names of people with that campaigner relationship type, but it gives nothing.

  • Entity: Contact
  • Action: get
{                                                                                                                                                                                           
    "select": ["display_name"],                                                                                                                                                               
    "where": [                                                                                                                                                                                                                                                                                                                                                        
      ["relationships.relationship_type_id", "=", 15]                                                                                                                                          
    ],                                                                                                                                                                                        
    "limit": 25,                                                                                                                                                                              
    "checkPermissions": false                                                                                                                                                                 
}
2

Not sure if this is what you're after but works for me; the 'join on' is a bit hidden in the 'Select' box.

CRM.api4('Relationship', 'get', {
  select: ["contact_a.display_name"],
  where: [["relationship_type_id", "=", 5]],
  limit: 25
}).then(function(relationships) {
  // do something with relationships array
}, function(failure) {
  // handle failure
})
1

For the part about making your first-step example work, use 'contact_a.display_name' instead of display_name.

Also it's missing from your example but it would be 'Relationship', 'get', at the very beginning, so the whole thing, to also get the org, is

$relationships = civicrm_api4('Relationship', 'get', [
  'select' => [
    'contact_a.display_name', 'contact_b.display_name'
  ],
  'where' => [
    ['relationship_type_id', '=', 15],
  ],
  'checkPermissions' => false,
]);
  • So are you saying I MUST do relationships from the relationships entity? Can't I start from contact, add relationship, add the related contact? – artfulrobot Nov 13 at 14:43
  • I find the api is always the reverse for joins. You kind of work backwards. See also civicrm.stackexchange.com/questions/28126/…. You could start from contact and write a procedural loop, but then it's not really a join. – Demerit Nov 13 at 14:43
  • Ok, so it it basically the case that APIv4 can do joins but only joins that are 1 deep? i.e. I can go Relationship to two contacts (and a relationship type); but I can't go contact → relationship → contact, because that's 2 deep. Or, for example, I couldn't get the phone number of the contact in your example (because that's relationship → contact → phone)? – artfulrobot Nov 13 at 15:22
  • I don't know the answer. Maybe there's a syntax for it. Or maybe with chaining, but then I get the impression from the docs that chaining isn't saving db calls vs doing a loop, just two or three less lines of code than writing your own loop. – Demerit Nov 13 at 15:55

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.