Answers.Body: <p>If you are happy working with the command line, install <a href="https://github.com/civicrm/cv" rel="nofollow noreferrer">cv</a> then you can run <code>cv scr /path/to/your/script.php</code></p>
  <p>To use your script with cv add <code>&lt;?php</code> as the first line.</p>
  <p>You'll want <code>'select' =&gt; ['id'],</code> not <code>'select' =&gt; ['primary_contact_id'],</code></p>
  <p>Once you get that far, you could just create the contributions directly instead of outputting the id and doing a separate import process. And from there you'll see it is only a few short steps to doing your entire import via the script with all the logic in one place!</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-11-03T04:38:59.877Z
Answers.Text: Very nice, thanks for the follow-on suggestions too.
Answers.UserDisplayName: Ken Williams
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-11-03T04:52:57.170Z
Answers.Text: Hmm, when I try to execute this I get a fatal error: `Unable to write to the "/opt/bitnami/drupal/sites/default/files/civicrm/templates_c" directory.`  Is there some other configuration I need to perform?  I'm not sure why it would be trying to write to the templates directory.
Answers.UserDisplayName: Ken Williams
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-11-03T12:25:04.360Z
Answers.Text: That's a permissions problem.  The files in templates_c will (mostly...) be owned by the id running the webserver.  Can you run your script as that user?  If not, you'll need to open up permissions on that directory so your id can write there.
Answers.UserDisplayName: Aidan
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-11-05T01:23:02.873Z
Answers.Text: Thanks, looks like I can do `sudo -u daemon \`which cv\` scr myscript.php`.  It's still not finding my contacts though, I think I'll post a new question about that.
Answers.UserDisplayName: Ken Williams
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-11-05T01:41:28.150Z
Answers.Text: Followup question is https://civicrm.stackexchange.com/questions/40660/make-api-queries-two-methods-failing .
Answers.UserDisplayName: Ken Williams
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-11-05T10:19:24.367Z
Answers.Text: Ah sorry, should have mentioned that!  See answer on your new question.
Answers.UserDisplayName: Aidan
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-11-02T13:39:50.640Z
Answers.LastActivityDate: 2021-11-02T13:39:50.640Z
Answers.UserDisplayName: Aidan
Body: <p>I have a long list of donations that I want to import.  My first step was to ensure that all the contacts in the list exist in CiviCRM - that's done, and I spent a while deduplicating the records.  Next, I imported all the donations that mapped cleanly using email addresses.  That worked well.</p>
  <p>Now I have a couple hundred donations left where the only identifying information is the contact's First Name and Last Name field.  It seems like the right thing to do now is to write a script that determines whether each of my First/Last pairs is unique among all contacts, and if so, pull the Contact ID so that I can put an ID in each line of the import data, and finally import the donations.  After this, I think there should only be a handful of donations to add manually.</p>
  <p>I was initially trying to write a script like so:</p>
  <pre><code>if (($handle = fopen($argv[1], &quot;r&quot;)) !== FALSE) {
      $header = fgetcsv($handle, 1000);
      fputcsv(STDOUT, $header);
      echo &quot;,contact_id\n&quot;;
      while (($data = fgetcsv($handle, 1000, &quot;,&quot;)) !== FALSE) {
          $row = array_combine($header, $data);
          
          $result = civicrm_api4('Contact', 'get', [
              'select' =&gt; ['primary_contact_id'],
              'where' =&gt; [['first_name', '=', $row['Firstname']],
                          ['last_name', '=', $row['Lastname']]]]);
  
          if (count($result) != 1) {
              continue;
          }
          row[] = $result[0]['id'];
          fputcsv(STDOUT, $row);
          echo &quot;\n&quot;;
      }
      fclose($handle);
  }
  </code></pre>
  <p>However, I got stuck trying to load the API library, and eventually I found the page <a href="https://docs.civicrm.org/dev/en/latest/extensions/advanced/" rel="nofollow noreferrer">https://docs.civicrm.org/dev/en/latest/extensions/advanced/</a> , which tells me I shouldn't be attempting to write a script like this (but it also says that its own self is out of date...).</p>
  <p>So what would be a good way to go about this?  I don't anticipate needing to do this exact task again, so I don't need to make it repeatable, but it would be good to learn what the &quot;paved path&quot; is here because I'm sure there will be other tasks to perform at various times.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-11-02T04:00:04.450Z
Comments.Text: if you import your first/last and add to a Group, then use that group to 'dedupe' you might be able to quickly deduce which have multiple matching contacts. and then do an export of ID and use vlookup to add the ID to your import?
Comments.UserDisplayName: petednz - fuzion
Tags: <api><contributions><php>
Title: Map firstname-lastname pairs to IDs
ThreadText: # Map firstname-lastname pairs to IDs
  
  (Asked by: username_0 on 2021-11-02)
  
  I have a long list of donations that I want to import.  My first step was to ensure that all the contacts in the list exist in CiviCRM - that's done, and I spent a while deduplicating the records.  Next, I imported all the donations that mapped cleanly using email addresses.  That worked well.
  Now I have a couple hundred donations left where the only identifying information is the contact's First Name and Last Name field.  It seems like the right thing to do now is to write a script that determines whether each of my First/Last pairs is unique among all contacts, and if so, pull the Contact ID so that I can put an ID in each line of the import data, and finally import the donations.  After this, I think there should only be a handful of donations to add manually.
  I was initially trying to write a script like so:
  if (($handle = fopen($argv[1], "r")) !== FALSE) {
      $header = fgetcsv($handle, 1000);
      fputcsv(STDOUT, $header);
      echo ",contact_id\n";
      while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
          $row = array_combine($header, $data);
          
          $result = civicrm_api4('Contact', 'get', [
              'select' => ['primary_contact_id'],
              'where' => [['first_name', '=', $row['Firstname']],
                          ['last_name', '=', $row['Lastname']]]]);
  
          if (count($result) != 1) {
              continue;
          }
          row[] = $result[0]['id'];
          fputcsv(STDOUT, $row);
          echo "\n";
      }
      fclose($handle);
  }
  
  However, I got stuck trying to load the API library, and eventually I found the page https://docs.civicrm.org/dev/en/latest/extensions/advanced/ , which tells me I shouldn't be attempting to write a script like this (but it also says that its own self is out of date...).
  So what would be a good way to go about this?  I don't anticipate needing to do this exact task again, so I don't need to make it repeatable, but it would be good to learn what the "paved path" is here because I'm sure there will be other tasks to perform at various times.
  
  **Tags:** api, contributions, php
  
  > **Comments:**
  > **username_1**: if you import your first/last and add to a Group, then use that group to 'dedupe' you might be able to quickly deduce which have multiple matching contacts. and then do an export of ID and use vlookup to add the ID to your import?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2021-11-02. Score: 2 | Accepted answer
  
  If you are happy working with the command line, install [cv](https://github.com/civicrm/cv) then you can run `cv scr /path/to/your/script.php`
  
  To use your script with cv add `php</code as the first line.`
  
  You'll want `'select' => ['id'],` not `'select' => ['primary_contact_id'],`
  
  Once you get that far, you could just create the contributions directly instead of outputting the id and doing a separate import process. And from there you'll see it is only a few short steps to doing your entire import via the script with all the logic in one place!
  
  > **Comments:**
  > **username_0**: Very nice, thanks for the follow-on suggestions too.  
  > **username_0**: Hmm, when I try to execute this I get a fatal error: `Unable to write to the "/opt/bitnami/drupal/sites/default/files/civicrm/templates_c" directory.`  Is there some other configuration I need to perform?  I'm not sure why it would be trying to write to the templates directory.  
  > **username_2**: That's a permissions problem.  The files in templates_c will (mostly...) be owned by the id running the webserver.  Can you run your script as that user?  If not, you'll need to open up permissions on that directory so your id can write there.  
  > **username_0**: Thanks, looks like I can do `sudo -u daemon \`which cv\` scr myscript.php`.  It's still not finding my contacts though, I think I'll post a new question about that.  
  > **username_0**: Followup question is https://civicrm.stackexchange.com/questions/40660/make-api-queries-two-methods-failing .  
  > **username_2**: Ah sorry, should have mentioned that!  See answer on your new question.  
  
  ---
  
  