0

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.

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,
          ));
        }
      }
    }
  }
}

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.