Fix hardcoded ID for Head of Household relationship
authorMatthew Wire (MJW Consulting) <mjw@mjwconsult.co.uk>
Fri, 23 Aug 2019 14:31:10 +0000 (15:31 +0100)
committerMatthew Wire (MJW Consulting) <mjw@mjwconsult.co.uk>
Fri, 23 Aug 2019 20:26:55 +0000 (21:26 +0100)
CRM/Contact/BAO/Relationship.php

index fc33ee7ef69c84c8490f97ae25dad0e701c20ba9..c8a6d98dadb563e1c85968a2c2ca4205b0820743 100644 (file)
@@ -329,8 +329,17 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship {
 
     // check if the relationship type is Head of Household then update the
     // household's primary contact with this contact.
-    if ($relationshipTypeID == 6) {
-      CRM_Contact_BAO_Household::updatePrimaryContact($relationship->contact_id_b, $relationship->contact_id_a);
+    try {
+      $headOfHouseHoldID = civicrm_api3('RelationshipType', 'getvalue', [
+        'return' => "id",
+        'name_a_b' => "Head of Household for",
+      ]);
+      if ($relationshipTypeID == $headOfHouseHoldID) {
+        CRM_Contact_BAO_Household::updatePrimaryContact($relationship->contact_id_b, $relationship->contact_id_a);
+      }
+    }
+    catch (Exception $e) {
+      // No "Head of Household" relationship found so we skip specific processing
     }
 
     if (!empty($params['id']) && self::isCurrentEmployerNeedingToBeCleared($relationship->toArray(), $params['id'], $relationshipTypeID)) {