Answers.Body: <p>Did you try <a href="https://docs.civicrm.org/user/en/latest/initial-set-up/users-contacts/#synchronize-users-to-contacts_1" rel="nofollow noreferrer">'Synchronize Users-to-Contacts'</a> from UI? You need to make sure you have unsupervised dedupe rule for individual set to email only before running <a href="https://docs.civicrm.org/user/en/latest/initial-set-up/users-contacts/#synchronize-users-to-contacts_1" rel="nofollow noreferrer">'Synchronize Users-to-Contacts'</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-07-13T18:14:48.120Z
Answers.Text: I forgot that this existed and when I Googled for it, somehow I did not find it. Unfortunately it is bidirectional so it is creating junk CiviCRM accounts for users who should be deleted but do not match members in CiviCRM. Easy enough to clean up with a search after the synchronization but I am going to see if I can deconstruct the code and make it run as a script just from the existing emails (in CiviCRM) to the users.
Answers.UserDisplayName: Josh
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-07-13T18:17:48.823Z
Answers.Text: I dont think it creates user account for civi contact. It maps cms user with civi contact based on email address. So you need to make sure you have unsupervised rule set to email only so that it doesn't create duplicates
Answers.UserDisplayName: Pradeep Nayak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-07-20T22:45:28.027Z
Answers.Text: HI Pradeep Nayak, I have played with it a lot and the problem is that it creates CiviCRM account for every Drupal user. This does not match our use case. Ideally we would want to go the other way around and find CiviCRM Contacts that match users and then make the match like I was trying to do above.  I think my problem is with my API call to add the ufmatch record. Thanks!
Answers.UserDisplayName: Josh
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-07-20T23:21:36.113Z
Answers.Text: What is your unsupervised Individual rule?
Answers.UserDisplayName: Pradeep Nayak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-07-26T23:13:56.617Z
Answers.Text: sorry it has taken a while to get back to you. It is email only. You can see https://onlinesharing18.s3.us-west-2.amazonaws.com/civicrm/unsupervised_rule.png Thank you!
Answers.UserDisplayName: Josh
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-07-13T14:19:14.600Z
Answers.LastActivityDate: 2020-07-13T14:19:14.600Z
Answers.UserDisplayName: Pradeep Nayak
Body: <p>I started with a fresh install of Drupal 8 and did a migration of about 5K users.  I am trying to force a recreation of the UF Matches. I cannot find a way to get CiviCRM to do this so I wrote the following code but this is not working either. Currently it delete the UFMatch from the table but it does not finish the final API call that creates the new match.</p>
  <pre><code>\Drupal::service('civicrm')-&gt;initialize();
  $q = \Drupal::entityQuery('user');
  $q-&gt;accessCheck(FALSE); //This is ran under cron
  $q-&gt;sort('created', 'DESC');
  $q-&gt;range(0,1);//Testing
  $users = $q-&gt;execute();
  foreach ($users as $uid) {
      $drupal_user = \Drupal\user\Entity\User::load($uid);
      $user_email = $drupal_user-&gt;getEmail();
      $result = civicrm_api3('UFMatch', 'get', [
          'sequential' =&gt; 1,
          'return' =&gt; [&quot;contact_id&quot;, &quot;uf_id&quot;],
          'uf_name' =&gt; $user_email,
      ]);
      print $user_email;
      if ($result['is_error'] == 0) {
          foreach ($result['values'] as $value) {
              $uf_id = $value['uf_id'];
              $contact_id = $value['contact_id'];
              $uf_match_table_id_result_array = civicrm_api3('UFMatch', 'get', [
                  'sequential' =&gt; 1,
                  'return' =&gt; [&quot;id&quot;],
                  'contact_id' =&gt; $contact_id,
              ]);
              $uf_match_table_id = $uf_match_table_id_result['id'];
              if ($uf_match_table_id_result_array['is_error'] == 0) {
                  foreach ($uf_match_table_id_result_array['values'] as $uf_match_table_id_result) {
                      $delete = civicrm_api3('UFMatch', 'delete', [
                          'id' =&gt; $uf_match_table_id,
                      ]);
  
                      if ($result['is_error'] == 0) {
                          $create_match = civicrm_api3('UFMatch', 'create', [
                              'uf_id' =&gt; $uf_id,
                              'uf_name' =&gt; $user_email,
                              'contact_id' =&gt; $contact_id,
                          ]);
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  <p>I think the issue might be the $uf_id variable that I am getting from the contact and using in the UFMatch create API call.</p>
  <p>Any ideas how to do this better or help with the code is greatly appreciated.</p>
  <p>Thanks,
  Steve</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-07-13T08:44:30.190Z
Comments.Text: did you a/ empty your uf_match table, b/ use the synch users option which should in theory then match a drupal user to the first civi ID with matching email
Comments.UserDisplayName: petednz - fuzion
Tags: <drupal><contacts>
Title: Regenerating UF Match
ThreadText: # Regenerating UF Match
  
  (Asked by: username_0 on 2020-07-13)
  
  I started with a fresh install of Drupal 8 and did a migration of about 5K users. I am trying to force a recreation of the UF Matches. I cannot find a way to get CiviCRM to do this so I wrote the following code but this is not working either. Currently it delete the UFMatch from the table but it does not finish the final API call that creates the new match.
  
  ```
  \Drupal::service('civicrm')->initialize();
  $q = \Drupal::entityQuery('user');
  $q->accessCheck(FALSE); //This is ran under cron
  $q->sort('created', 'DESC');
  $q->range(0,1);//Testing
  $users = $q->execute();
  foreach ($users as $uid) {
      $drupal_user = \Drupal\user\Entity\User::load($uid);
      $user_email = $drupal_user->getEmail();
      $result = civicrm_api3('UFMatch', 'get', [
          'sequential' => 1,
          'return' => ["contact_id", "uf_id"],
          'uf_name' => $user_email,
      ]);
      print $user_email;
      if ($result['is_error'] == 0) {
          foreach ($result['values'] as $value) {
              $uf_id = $value['uf_id'];
              $contact_id = $value['contact_id'];
              $uf_match_table_id_result_array = civicrm_api3('UFMatch', 'get', [
                  'sequential' => 1,
                  'return' => ["id"],
                  'contact_id' => $contact_id,
              ]);
              $uf_match_table_id = $uf_match_table_id_result['id'];
              if ($uf_match_table_id_result_array['is_error'] == 0) {
                  foreach ($uf_match_table_id_result_array['values'] as $uf_match_table_id_result) {
                      $delete = civicrm_api3('UFMatch', 'delete', [
                          'id' => $uf_match_table_id,
                      ]);
  
                      if ($result['is_error'] == 0) {
                          $create_match = civicrm_api3('UFMatch', 'create', [
                              'uf_id' => $uf_id,
                              'uf_name' => $user_email,
                              'contact_id' => $contact_id,
                          ]);
                      }
                  }
              }
          }
      }
  }
  
  ```
  
  I think the issue might be the $uf\_id variable that I am getting from the contact and using in the UFMatch create API call.
  
  Any ideas how to do this better or help with the code is greatly appreciated.
  
  Thanks,
  username_0
  
  **Tags:** drupal, contacts
  
  > **Comments:**
  > **username_1**: did you a/ empty your uf_match table, b/ use the synch users option which should in theory then match a drupal user to the first civi ID with matching email  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2020-07-13. Score: 1
  
  Did you try ['Synchronize Users-to-Contacts'](https://docs.civicrm.org/user/en/latest/initial-set-up/users-contacts/#synchronize-users-to-contacts_1) from UI? You need to make sure you have unsupervised dedupe rule for individual set to email only before running ['Synchronize Users-to-Contacts'](https://docs.civicrm.org/user/en/latest/initial-set-up/users-contacts/#synchronize-users-to-contacts_1)
  
  > **Comments:**
  > **username_3**: I forgot that this existed and when I Googled for it, somehow I did not find it. Unfortunately it is bidirectional so it is creating junk CiviCRM accounts for users who should be deleted but do not match members in CiviCRM. Easy enough to clean up with a search after the synchronization but I am going to see if I can deconstruct the code and make it run as a script just from the existing emails (in CiviCRM) to the users.  
  > **username_2**: I dont think it creates user account for civi contact. It maps cms user with civi contact based on email address. So you need to make sure you have unsupervised rule set to email only so that it doesn't create duplicates  
  > **username_3**: HI username_2, I have played with it a lot and the problem is that it creates CiviCRM account for every Drupal user. This does not match our use case. Ideally we would want to go the other way around and find CiviCRM Contacts that match users and then make the match like I was trying to do above.  I think my problem is with my API call to add the ufmatch record. Thanks!  
  > **username_2**: What is your unsupervised Individual rule?  
  > **username_3**: sorry it has taken a while to get back to you. It is email only. You can see https://onlinesharing18.s3.us-west-2.amazonaws.com/civicrm/unsupervised_rule.png Thank you!  
  
  ---
  
  