Answers.Body: <p>You would need to write a custom extension or module. 
  in this extension or module, implement hook_civicrm_post()...</p>
  
  <p>Drupal module's are the simplest, if you are integrating with Drupal that is</p>
  
  <p>so for example</p>
  
  <pre><code>function MYMODULE_civicrm_post($op, $objectName, $objectId, &amp;$objectRef) {
    // if location type  is Home (location_type_id:1), operation is create, and object type is Address
    if ($op == 'create' &amp;&amp; $objectName == 'Address' &amp;&amp; $objectRef-&gt;location_type_id == 1) {
      // look for other addresses with the same street address, city, state, postal code, zip, and country
      $result = civicrm_api3('Address', 'get', array(
        'sequential' =&gt; 1,
        'street_address' =&gt; $objectRef-&gt;street_address,
        'city' =&gt; $objectRef-&gt;city,
        "state_province_id" =&gt; $objectRef-&gt;state_province_id,
        "postal_code" =&gt;  $objectRef-&gt;postal_code,
        "country_id" =&gt;  $objectRef-&gt;country_id,
        'contact_id' =&gt; array('!=' =&gt; $objectRef-&gt;contact_id),
      ));
      if(!empty($result['count'])) {
        foreach ($result['values'] as $value) {
          // check for existing relationship between contacts
          $result1 = civicrm_api3('Relationship', 'get', array(
            'sequential' =&gt; 1,
            'contact_id_a' =&gt; $value['contact_id'],
            'contact_id_b' =&gt; $objectRef-&gt;contact_id,
            'relationship_type_id' =&gt; 8, // change this to the id of the type of relationship you want to create
          ));
          $result2 = civicrm_api3('Relationship', 'get', array(
            'sequential' =&gt; 1,
            'contact_id_a' =&gt; $objectRef-&gt;contact_id,
            'contact_id_b' =&gt; $value['contact_id'],
            'relationship_type_id' =&gt; 8, // change this to the id of the type of relationship you want to create
          ));
  
          // if no existing relationship found create the relationship
          if($result1['count'] == 0 &amp;&amp; $result2['count'] == 0) {
            $create_result = civicrm_api3('Relationship', 'create', array(
              'sequential' =&gt; 1,
              'contact_id_a' =&gt; $objectRef-&gt;contact_id,
              'contact_id_b' =&gt; $value['contact_id'],
              'relationship_type_id' =&gt; 8, // change this to the id of the type of relationship you want to create
              'is_active' =&gt; 1,
            ));
          }
        }
      }
    }
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-24T23:25:58.967Z
Answers.LastActivityDate: 2017-06-24T23:25:58.967Z
Answers.UserDisplayName: jackrabbithanna
Body: <p>Is there a way to automatically create a relationship if 2 or more address in the DB is exactly the same?</p>
  
  <p>I want to create the relationship if two or more people are living in same house. </p>
  
Tags: <webform-civicrm><civireport><relationships>
Title: how to automatically creates a relationship
ThreadText: # how to automatically creates a relationship
  
  (Asked by: username_0 on 2017-06-16)
  
  Is there a way to automatically create a relationship if 2 or more address in the DB is exactly the same?
  
  I want to create the relationship if two or more people are living in same house.
  
  **Tags:** webform-civicrm, civireport, relationships
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-06-24. Score: 1
  
  You would need to write a custom extension or module.
  in this extension or module, implement hook\_civicrm\_post()...
  
  Drupal module's are the simplest, if you are integrating with Drupal that is
  
  so for example
  
  ```
  function MYMODULE_civicrm_post($op, $objectName, $objectId, &$objectRef) {
    // if location type  is Home (location_type_id:1), operation is create, and object type is Address
    if ($op == 'create' && $objectName == 'Address' && $objectRef->location_type_id == 1) {
      // look for other addresses with the same street address, city, state, postal code, zip, and country
      $result = civicrm_api3('Address', 'get', array(
        'sequential' => 1,
        'street_address' => $objectRef->street_address,
        'city' => $objectRef->city,
        "state_province_id" => $objectRef->state_province_id,
        "postal_code" =>  $objectRef->postal_code,
        "country_id" =>  $objectRef->country_id,
        'contact_id' => array('!=' => $objectRef->contact_id),
      ));
      if(!empty($result['count'])) {
        foreach ($result['values'] as $value) {
          // check for existing relationship between contacts
          $result1 = civicrm_api3('Relationship', 'get', array(
            'sequential' => 1,
            'contact_id_a' => $value['contact_id'],
            'contact_id_b' => $objectRef->contact_id,
            'relationship_type_id' => 8, // change this to the id of the type of relationship you want to create
          ));
          $result2 = civicrm_api3('Relationship', 'get', array(
            'sequential' => 1,
            'contact_id_a' => $objectRef->contact_id,
            'contact_id_b' => $value['contact_id'],
            'relationship_type_id' => 8, // change this to the id of the type of relationship you want to create
          ));
  
          // if no existing relationship found create the relationship
          if($result1['count'] == 0 && $result2['count'] == 0) {
            $create_result = civicrm_api3('Relationship', 'create', array(
              'sequential' => 1,
              'contact_id_a' => $objectRef->contact_id,
              'contact_id_b' => $value['contact_id'],
              'relationship_type_id' => 8, // change this to the id of the type of relationship you want to create
              'is_active' => 1,
            ));
          }
        }
      }
    }
  }
  
  ```
  
  
  ---
  
  