Answers.Body: <p>You need to implement something like below</p>
  
  <pre><code>$result = civicrm_api3('Contact', 'create', [
    'first_name' =&gt; "first name",
    'last_name' =&gt; "last name",
    'email' =&gt; "pradeep@test.com",
    'external_identifier' =&gt; "S-123456",
    'options' =&gt; ['match' =&gt; "external_identifier"],
  ]);
  </code></pre>
  
  <p>Make sure you pass <code>external_identifier</code> in api params. </p>
  
  <p>So basically above code snippet will first check if external_identifier "S-123456" is present in database and get its id if present and update the contact, if not present then new contact will be created.</p>
  
  <p>HTH</p>
  
  <p>Pradeep</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-11T21:58:24.327Z
Answers.Text: Ya, that is what I am doing and I get "CiviCRM_API3_Exception: Ambiguous match criteria in civicrm_api3() (line 45 of /var/www/html/sites/all/modules/civicrm/api/api.php)."  This is a shortened version of what my API call looks like      $result = civicrm_api3('Contact', 'create', [
          'contact_type' => 'Individual',
          'contact_sub_type' => 'Student',
          'last_name' => $student_last_name,
          'first_name' => $student_first_name,
          'external_identifier' => $student_id,
          'options' => ['match' => "external_identifier"],
      ]);  Thanks!
Answers.UserDisplayName: Josh
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-11T22:06:00.800Z
Answers.Text: It means it's matching on more than one. Since there's a unique db index on that field my guess is you're passing a null $student_id ?
Answers.UserDisplayName: Demerit
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-11T23:44:13.760Z
Answers.Text: CiviCRM_API3_Exception: Ambiguous match criteria usually occurs when the match criteria field is passed with null value or empty. In your case $student_last_name must be null or empty hence the api throughs error.
Answers.UserDisplayName: Pradeep Nayak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-12T03:29:13.690Z
Answers.Text: But the only match criteria is the external id, right? What all must be included to be a match? There could be some new students in the CSV.
Answers.UserDisplayName: Josh
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-12T03:59:37.913Z
Answers.Text: Change the options line to `'options' => ($student_id ? ['match' => "external_identifier"] : []),`. In other words, don't pass in the match option if student_id is blank.
Answers.UserDisplayName: Demerit
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-12T07:34:04.623Z
Answers.Text: Yes, agreed with Demerit, donot pass in match if the external identifier is empty or null
Answers.UserDisplayName: Pradeep Nayak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-11T21:06:37.793Z
Answers.LastActivityDate: 2019-01-11T21:06:37.793Z
Answers.UserDisplayName: Pradeep Nayak
Body: <p>I am writing a script that updates contacts from an external source when their external identifier is equal to the contact being imported.  My research shows (and the API explorer confirms) that it should look like this:</p>
  
  <pre><code>'options' =&gt; ['match' =&gt; "external_identifier"],
  </code></pre>
  
  <p>I get the error  "CiviCRM_API3_Exception: Ambiguous match criteria in civicrm_api3() (line 45 of /var/www/html/sites/all/modules/civicrm/api/api.php)." </p>
  
  <p>Can anyone suggest a solution for what I am doing wrong?</p>
  
  <p>Thanks,
  Josh</p>
  
Tags: <api><duplicate-contacts>
Title: API 'match' => "external_identifier"
ThreadText: # API 'match' => "external_identifier"
  
  (Asked by: username_0 on 2019-01-11)
  
  I am writing a script that updates contacts from an external source when their external identifier is equal to the contact being imported.  My research shows (and the API explorer confirms) that it should look like this:
  'options' => ['match' => "external_identifier"],
  
  I get the error  "CiviCRM_API3_Exception: Ambiguous match criteria in civicrm_api3() (line 45 of /var/www/html/sites/all/modules/civicrm/api/api.php)." 
  Can anyone suggest a solution for what I am doing wrong?
  Thanks,
  username_0
  
  **Tags:** api, duplicate-contacts
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-01-11. Score: 2
  
  You need to implement something like below
  $result = civicrm_api3('Contact', 'create', [
    'first_name' => "first name",
    'last_name' => "last name",
    'email' => "pradeep@test.com",
    'external_identifier' => "S-123456",
    'options' => ['match' => "external_identifier"],
  ]);
  
  Make sure you pass external_identifier in api params. 
  So basically above code snippet will first check if external_identifier "S-123456" is present in database and get its id if present and update the contact, if not present then new contact will be created.
  HTH
  Pradeep
  
  > **Comments:**
  > **username_0**: Ya, that is what I am doing and I get "CiviCRM_API3_Exception: Ambiguous match criteria in civicrm_api3() (line 45 of /var/www/html/sites/all/modules/civicrm/api/api.php)."  This is a shortened version of what my API call looks like      $result = civicrm_api3('Contact', 'create', [         'contact_type' => 'Individual',         'contact_sub_type' => 'Student',         'last_name' => $student_last_name,         'first_name' => $student_first_name,         'external_identifier' => $student_id,         'options' => ['match' => "external_identifier"],     ]);  Thanks!  
  > **username_2**: It means it's matching on more than one. Since there's a unique db index on that field my guess is you're passing a null $student_id ?  
  > **username_1**: CiviCRM_API3_Exception: Ambiguous match criteria usually occurs when the match criteria field is passed with null value or empty. In your case $student_last_name must be null or empty hence the api throughs error.  
  > **username_0**: But the only match criteria is the external id, right? What all must be included to be a match? There could be some new students in the CSV.  
  > **username_2**: Change the options line to `'options' => ($student_id ? ['match' => "external_identifier"] : []),`. In other words, don't pass in the match option if student_id is blank.  
  > **username_1**: Yes, agreed with username_2, donot pass in match if the external identifier is empty or null  
  
  ---
  
  