Is there a way for this: When a new contact is created (through drupal or backend), auto assign a relationship to an organization if one exists with the same email domain as the contact's email domain.

Makes sense right? If I'm newly on-boarded to my company, I'd like to just register on the site and it all be set up and working. Most of our members inherit their membership through their Employer and we'd like to manually have this done since this is starting to be an administrative burden.

  • What if two organizations exist that share the same email domain? For instance, what if two have a gmail account?
    – Zachary
    Jun 15, 2019 at 1:34
  • 1
    Good question, however we don't have any cases like that. All of our organizations have unique domains.. and if we have an instance where some smaller org uses Gmail, or something similar, we'd probably treat it as an edge case and have another process.
    – VoRCeK
    Jun 15, 2019 at 5:03

2 Answers 2


Thanks everyone! I solved it totally with CiviRules extension. I just used a combination of the built-in API explorer & the extension's documentation to figure out how to create my own Condition & Action.

The condition checks if that domain exists for an Organization. I set it to work ONLY if it finds 1 (just to make it work simply for now) and the contact is an Individual (no Org to Org relationships). If true, the Action then creates the relationship.

To make this work, when we set up an Org.. we make a dummy email their primary/main email and put it on hold so no communications go out to it.. like this: [email protected]

I'll share the big stuff. I'm not a coder, so if someone finds a better way to do this.. let me know.



  public function isConditionValid(CRM_Civirules_TriggerData_TriggerData $triggerData) {
    /** $this->logCondition('my con log message', $triggerData, \PSR\Log\LogLevel::INFO); */
    $contactId = $triggerData->getContactId();
    $contactTypeParams = array('return' => "contact_type",'id' => $contactId);
    $contactType = civicrm_api3('Contact', 'Getvalue', $contactTypeParams);
    $contactEmailParams = array('return' => "email",'id' => $contactId);
    $contactEmail = civicrm_api3('Contact', 'getvalue', $contactEmailParams);
    $contactEmailDomain = substr(strrchr($contactEmail, "@"), 1);
    $orgEmailDomainCountParams = array('email' => ['LIKE' => "%" . $contactEmailDomain],'contact_type' => "Organization");
    $orgEmailDomainCount = civicrm_api3('Contact', 'getcount', $orgEmailDomainCountParams);
    if ($orgEmailDomainCount = 1 && $contactType = "Individual"){
        return TRUE;
    } else {
    return FALSE;


  public function processAction(CRM_Civirules_TriggerData_TriggerData $triggerData) {
     /** $this->logAction('my act log message', $triggerData, \PSR\Log\LogLevel::INFO);  */
    $contactId = $triggerData->getContactId();
    $contactEmailParams = array('return' => "email",'id' => $contactId);
    $contactEmail = civicrm_api3('Contact', 'getvalue', $contactEmailParams);
    $contactEmailDomain = explode('@', $contactEmail)[1];
    $orgContactIdParams = array('return' => "id",'email' => ['LIKE' => "%" . $contactEmailDomain],'contact_type' => "Organization");
    $orgContactId = civicrm_api3('Contact', 'getvalue', $orgContactIdParams);
    civicrm_api3('Relationship', 'create', ['contact_id_a' => $contactId,'contact_id_b' => $orgContactId,'relationship_type_id' => 5,'is_active' => 1,'is_permission_a_b' => 2,]);
    civicrm_api3('Contact', 'create', ['id' => $contactId,'employer_id' => $orgContactId,]);
  • 1
    right on! thanks for sharing the solution
    – ericG
    Jun 17, 2019 at 16:29

you could write an extension, or module that uses the civicrm_post hook; when a new contact is created, look at the email; do an api call to see if that domain exists in one of your valid contacts; if so, create the relationship between the two cids.

It might be easier with the civicrm_entity module and its hooks instead of the post hook. Might even be able to do it entirely with civirules extension, civicrm_entity and rules modules.

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.