I want to create some Relationships of type "Case Coordinator" via API Call in CiviCRM.

When I create two relationships with the same contact_id_a, relationship_type_id, is_active and case_id values, but with a different contact_id_b value, I nevertheless get the error "Duplicate Relationship thrown in .../public/sites/all/modules/civicrm/api/api.php on line 134".

I have been checking the code in public/sites/all/modules/civicrm/CRM/Contact/BAO/Relationship.php and it seems to check the sameness of contact_id_a and contact_id_b correctly:

public static function checkDuplicateRelationship(&$params, $id, $contactId = 0, $relationshipId = 0) {
    $queryString .=
      " AND ( ( contact_id_a = " . CRM_Utils_Type::escape($id, 'Integer') .
      " AND contact_id_b = " . CRM_Utils_Type::escape($contactId, 'Integer') .
      " ) OR ( contact_id_a = " . CRM_Utils_Type::escape($contactId, 'Integer') .
      " AND contact_id_b = " . CRM_Utils_Type::escape($id, 'Integer') . " ) ) ";

Only when I add a distinct start_date to the API Create Relationship call, CiviCRM accepts the call and creates the relationship properly.

This is ok as a workaround, but I wonder if anybody knows why the check on Contact A and Contact B in the code is not working here? As obviously two relationships are not a duplicate if they exist between a different pair of contacts.

  • have you confirmed if you hit the same issue with a non-civicase relationship? wondering if civicase throws in some extra hurdles
    – petednz - fuzion
    May 27, 2022 at 23:12

1 Answer 1


I can't reproduce the fail. Both api3 and api4 seem to work fine using the params as described. Perhaps there is something else involved on that site?

e.g. both these work when there is already an existing relationship of the same type on the case with a different contact:

cv ev --user=admin "civicrm_api3('Relationship','create', [contact_id_a => 8, contact_id_b => 9, relationship_type_id => 14, is_active =>1 , case_id => 1]);"

cv ev --user=admin "\Civi\Api4\Relationship::create()->addValue('contact_id_a', 8)->addValue('contact_id_b', 10)->addValue('relationship_type_id', 14)->addValue('case_id', 1)->execute();"

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.