Answers.Body: <p>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. </p>
  
  <p>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. </p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-06-15T21:12:56.340Z
Answers.LastActivityDate: 2019-06-15T21:12:56.340Z
Answers.UserDisplayName: ericG
Answers.Body: <p>Thanks everyone!  I solved it totally with CiviRules extension.  I just used a combination of the built-in API explorer &amp; the extension's documentation to figure out how to create my own Condition &amp; Action.</p>
  
  <p>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.</p>
  
  <p>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: dummy@domain.com</p>
  
  <p>I'll share the big stuff.  I'm not a coder, so if someone finds a better way to do this.. let me know.    </p>
  
  <p><a href="https://docs.civicrm.org/civirules/en/latest/" rel="nofollow noreferrer">https://docs.civicrm.org/civirules/en/latest/</a></p>
  
  <pre><code>    Condition:
  
    public function isConditionValid(CRM_Civirules_TriggerData_TriggerData $triggerData) {
      /** $this-&gt;logCondition('my con log message', $triggerData, \PSR\Log\LogLevel::INFO); */
      $contactId = $triggerData-&gt;getContactId();
      $contactTypeParams = array('return' =&gt; "contact_type",'id' =&gt; $contactId);
      $contactType = civicrm_api3('Contact', 'Getvalue', $contactTypeParams);
      $contactEmailParams = array('return' =&gt; "email",'id' =&gt; $contactId);
      $contactEmail = civicrm_api3('Contact', 'getvalue', $contactEmailParams);
      $contactEmailDomain = substr(strrchr($contactEmail, "@"), 1);
      $orgEmailDomainCountParams = array('email' =&gt; ['LIKE' =&gt; "%" . $contactEmailDomain],'contact_type' =&gt; "Organization");
      $orgEmailDomainCount = civicrm_api3('Contact', 'getcount', $orgEmailDomainCountParams);
      if ($orgEmailDomainCount = 1 &amp;&amp; $contactType = "Individual"){
          return TRUE;
      } else {
      return FALSE;
      }
  
      Action:
  
    public function processAction(CRM_Civirules_TriggerData_TriggerData $triggerData) {
       /** $this-&gt;logAction('my act log message', $triggerData, \PSR\Log\LogLevel::INFO);  */
      $contactId = $triggerData-&gt;getContactId();
      $contactEmailParams = array('return' =&gt; "email",'id' =&gt; $contactId);
      $contactEmail = civicrm_api3('Contact', 'getvalue', $contactEmailParams);
      $contactEmailDomain = explode('@', $contactEmail)[1];
      $orgContactIdParams = array('return' =&gt; "id",'email' =&gt; ['LIKE' =&gt; "%" . $contactEmailDomain],'contact_type' =&gt; "Organization");
      $orgContactId = civicrm_api3('Contact', 'getvalue', $orgContactIdParams);
      civicrm_api3('Relationship', 'create', ['contact_id_a' =&gt; $contactId,'contact_id_b' =&gt; $orgContactId,'relationship_type_id' =&gt; 5,'is_active' =&gt; 1,'is_permission_a_b' =&gt; 2,]);
      civicrm_api3('Contact', 'create', ['id' =&gt; $contactId,'employer_id' =&gt; $orgContactId,]);
    }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-06-17T16:29:22.157Z
Answers.Text: right on! thanks for sharing the solution
Answers.UserDisplayName: ericG
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-06-16T01:06:03.507Z
Answers.LastActivityDate: 2019-06-23T15:19:34.187Z
Answers.LastEditDate: 2019-06-23T15:19:34.187Z
Answers.UserDisplayName: VoRCeK
Body: <p>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.  </p>
  
  <p>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.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-06-15T01:34:55.343Z
Comments.Text: What if two organizations exist that share the same email domain? For instance, what if two have a gmail account?
Comments.UserDisplayName: Zachary
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-06-15T05:03:24.517Z
Comments.Text: 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.
Comments.UserDisplayName: VoRCeK
Tags: <drupal><civimember><relationships>
Title: At signup, auto assign organization:contact relationship based on email domain being the same
ThreadText: # At signup, auto assign organization:contact relationship based on email domain being the same
  
  (Asked by: username_0 on 2019-06-14)
  
  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.
  
  **Tags:** drupal, civimember, relationships
  
  > **Comments:**
  > **username_1**: What if two organizations exist that share the same email domain? For instance, what if two have a gmail account?  
  > **username_0**: 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.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2019-06-16. Score: 5 | Accepted answer
  
  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: dummy@domain.com
  I'll share the big stuff.  I'm not a coder, so if someone finds a better way to do this.. let me know.    
  https://docs.civicrm.org/civirules/en/latest/
      Condition:
  
    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;
      }
  
      Action:
  
    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,]);
    }
  
  > **Comments:**
  > **username_2**: right on! thanks for sharing the solution  
  
  ---
  
  ### Answer by username_2 on 2019-06-15. Score: 1
  
  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.
  
  
  ---
  
  