Answers.Body: <p>Welcome to SE!  It looks like you are trying to create an Individual and that your API call doesn't have all the necessary data.</p>
  <p>One option is to print out the API parameters just before you make the API call and check it has what you expect.  See <a href="https://docs.civicrm.org/dev/en/latest/tools/debugging/#printing-php-variables" rel="nofollow noreferrer">https://docs.civicrm.org/dev/en/latest/tools/debugging/#printing-php-variables</a></p>
  <p>If you edit the question and include your code, or a link to it, then we can offer more specific suggestions.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-02-10T11:28:54.340Z
Answers.Text: Thanks for your help. I will get back to with the code example and edit my question. However I need to wait for my client to refresh the test version of CIVI and make a new test version as an exact copy from live so I can recreate the bug and test.
Answers.UserDisplayName: tim golding
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-02-10T16:45:27.943Z
Answers.Text: I have added some code. Although i wont be able to debug as i have said this only happens on live CIVI so i need to refresh our test version. I will let you know when this happens
Answers.UserDisplayName: tim golding
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-02-11T17:51:50.887Z
Answers.Text: Could you please clarify the context in which you are running this code, and where the function civicrm_contact_create is defined? That function does not exist in modern versions of CiviCRM.
Answers.UserDisplayName: Noah Miller
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-02-12T09:57:19.340Z
Answers.Text: Thanks for adding the code. Depending how you are running it, I'd add `print_r($account)` at the top since I suspect that does not contain what you expect - in which case the question moves further back to how `$account` is being set.
Answers.UserDisplayName: Aidan
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-02-12T11:02:07.503Z
Answers.Text: Hi @Aidan account is defined by Drupal it is an abject for the users signed up to drupal and is part of the hook. 
  
  https://api.drupal.org/api/drupal/modules!user!user.api.php/function/hook_user_presave/7.x
  
  It is passed to this hook, we can add additional fields to the user object. Hope that makes sense?
Answers.UserDisplayName: tim golding
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-02-15T09:37:14.537Z
Answers.Text: Hi I have now figured out that my statement that this only happens on the live instance of CIVI. Sorry for confusion it happens when debug mode is on we define debug mode in the admin by ticking a box. This is so we can send debug info to the log. for example
  
  if (variable_get('civicrm_api_connect_debug', false)) {
      watchdog('civicrm', '<pre>' . print_r($response, 1) . '</pre>');
    }
  
  It happens when debug is off. So will try to figure out how this can have an effect on that.
Answers.UserDisplayName: tim golding
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-02-10T10:51:48.300Z
Answers.LastActivityDate: 2021-02-10T10:51:48.300Z
Answers.UserDisplayName: Aidan
Body: <p>Hi I spent a long time testing my code and checking it worked on our test CIVI. On the test version of the CIVI CRM the requests work no problems. As soon as I switch to live and connect to the real CIVI I'm troubled with this error. Since this works perfectly on the test CIVI CRM I'm assuming there must be some setting different within CIVI? How can I debug this? When it works on my testing environment? Does anyone know what I need to change within CIVI.</p>
  <p>I have a feeling it is related to this bit where it tries to save the users email as Main. On live CIVI it creates two users with one as email in main one in home. On testing however (the tet CIVI) it works as expected.</p>
  <pre><code>    $data['contact_type'] = 'Individual';
      $data['first_name'] = $account-&gt;field_first_name['und'][0]['value'];
      $data['last_name'] = $account-&gt;field_last_name['und'][0]['value'];
      $data['email'] = $account-&gt;mail;
      $data['location_type_id'] = 'Main';
  
      if (isset($account-&gt;field_company_name['und'][0]['value'])) {
        $data['current_employer'] = $account-&gt;field_company_name['und'][0]['value'];
      }
  
      $response = civicrm_contact_create($data);
      
  
      if (!$response-&gt;is_error) {
        $contact = reset($response-&gt;values);
        $contact_id = $contact-&gt;id;
  
       // search for the email to get the id.
        $email_search_params = array(
          'contact_id' =&gt; $contact_id,
          'location_type_id' =&gt; 'Home',
        );
        $email_search_response = civicrm_email_search($email_search_params);
        $email_search_params['email'] = $account-&gt;mail;
        if (!$email_search_response-&gt;is_error &amp;&amp; count($email_search_response-&gt;values)) {
          $email = reset($email_search_response-&gt;values);
          // Add email to Main.
          $params = array(
            'id' =&gt; $email-&gt;id,
            'location_type_id' =&gt; 'Main'
          );
          $response2 = civicrm_email_create($params);
  
         }
        }
  
        // Add phone using Phone create CiviCRM API
        if ($account-&gt;field_telephone_number['und'][0]['value']) {
  
          $params = array(
            'phone' =&gt; $account-&gt;field_telephone_number['und'][0]['value'],
            'contact_id' =&gt; $contact_id,
            'location_type_id' =&gt; 'Main', // Other until main form completed
          );
          $response3 = civicrm_phone_create($params);
  
          if (variable_get('civicrm_api_connect_debug', false)) {   
            watchdog('civicrm', '&lt;pre&gt;' . print_r($response3, 1) . '&lt;/pre&gt;');
          }
        }
  
        $edit['field_civi_id']['und'][0]['value'] = $contact_id;
        // Default primary contact to the web administrator
        $edit['field_primary_contact_first_name']['und'][0]['value'] = $account-&gt;field_first_name['und'][0]['value'];
        $edit['field_primary_contact_last_name']['und'][0]['value'] = $account-&gt;field_last_name['und'][0]['value'];
  </code></pre>
  <p>This is an update:</p>
  <p>I have added the code for civi_create_contact as requested</p>
  <pre><code>function civicrm_contact_create($params) {
    return civicrm_api_query('Contact', 'create', $params);
  }
  </code></pre>
  <p>And this is the code for civicrm_api_query:</p>
  <pre><code>function civicrm_api_query($entity, $action, $data = array()) {
        $url = civicrm_api_connect_get_base_url() . &quot;/sites/all/modules/civicrm/extern/rest.php&quot;;
  
    $keys = civicrm_api_connect_auth();
  
    $params = array(
      'entity' =&gt; $entity,
      'action' =&gt; $action,
      'api_key' =&gt; $keys['apikey'],
      'key' =&gt; $keys['sitekey'],
      'json' =&gt; 1,
    );
  
    $headers = array(
      'Content-Type' =&gt; 'application/x-www-form-urlencoded'
    );
  
    // Send the HTTP basic authentication headers if we are in test mode.
    if (variable_get('civicrm_api_connect_test', false)) {
        $headers['Authorization'] = 'Basic ' .
          base64_encode(
            variable_get('civicrm_api_connect_access_username') . ':' .
            /*decrypt*/(variable_get('civicrm_api_connect_access_password')));
    }
  
    $params = array_merge($params, $data);
  
    $query = drupal_http_build_query($params, '', '&amp;');
  
    if ($action == 'get') {
      $url .= '?' . $query;
      $method = 'GET';
    }
  
    if ($action == 'create') {
      $method = 'POST';
    }
  
    $data = array(
      'method' =&gt; $method,
      'headers' =&gt; $headers,
      'data' =&gt; $query,
    );
  
    // Trace the submitted data if we're in debug mode
    if (variable_get('civicrm_api_connect_debug', false)) {
      watchdog('civicrm', '&lt;pre&gt;' . print_r($data, 1) . '&lt;/pre&gt;');
    }
  
    $response = drupal_http_request($url, $data);
  
    // Trace the received data if we're in debug mode
    if (variable_get('civicrm_api_connect_debug', false)) {
      watchdog('civicrm', '&lt;pre&gt;' . print_r($response, 1) . '&lt;/pre&gt;');
    }
  
    if ($response-&gt;code == 200) {
      $data = json_decode($response-&gt;data);
  
      // Trace the response if it's an error and we haven't already traced
      // it through being in debug mode.
      if ($data-&gt;is_error) {
        drupal_set_message(t('CiviCRM error @error', array('@error' =&gt; $data-&gt;error_message)), 'error');
  
        if (variable_get('civicrm_api_connect_debug', false)) {
          watchdog('civicrm_error', $data-&gt;error_message);
          //watchdog('civicrm_error', '&lt;pre&gt;' . print_r($params,1) . '&lt;/pre&gt;');
        }
        return false;
      }
      else {
        return $data;
      }
    }
    else {
      watchdog('civicrm_error', 'CiviCRM fatal error');
      return false;
    }
  }
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-02-17T00:18:30.313Z
Comments.Text: goulding, welcome to the stack! Your post's subject should ideally be phrased as a question. Please rephrase it?
Comments.UserDisplayName: JohnFF
Tags: <civicrm.settings.php>
Title: Mandatory Key(s) missing params array:one of (first_name, last_name, email, display_name)
ThreadText: # Mandatory Key(s) missing params array:one of (first_name, last_name, email, display_name)
  
  (Asked by: username_0 on 2021-02-10)
  
  Hi I spent a long time testing my code and checking it worked on our test CIVI. On the test version of the CIVI CRM the requests work no problems. As soon as I switch to live and connect to the real CIVI I'm troubled with this error. Since this works perfectly on the test CIVI CRM I'm assuming there must be some setting different within CIVI? How can I debug this? When it works on my testing environment? Does anyone know what I need to change within CIVI.
  I have a feeling it is related to this bit where it tries to save the users email as Main. On live CIVI it creates two users with one as email in main one in home. On testing however (the tet CIVI) it works as expected.
      $data['contact_type'] = 'Individual';
      $data['first_name'] = $account->field_first_name['und'][0]['value'];
      $data['last_name'] = $account->field_last_name['und'][0]['value'];
      $data['email'] = $account->mail;
      $data['location_type_id'] = 'Main';
  
      if (isset($account->field_company_name['und'][0]['value'])) {
        $data['current_employer'] = $account->field_company_name['und'][0]['value'];
      }
  
      $response = civicrm_contact_create($data);
      
  
      if (!$response->is_error) {
        $contact = reset($response->values);
        $contact_id = $contact->id;
  
       // search for the email to get the id.
        $email_search_params = array(
          'contact_id' => $contact_id,
          'location_type_id' => 'Home',
        );
        $email_search_response = civicrm_email_search($email_search_params);
        $email_search_params['email'] = $account->mail;
        if (!$email_search_response->is_error && count($email_search_response->values)) {
          $email = reset($email_search_response->values);
          // Add email to Main.
          $params = array(
            'id' => $email->id,
            'location_type_id' => 'Main'
          );
          $response2 = civicrm_email_create($params);
  
         }
        }
  
        // Add phone using Phone create CiviCRM API
        if ($account->field_telephone_number['und'][0]['value']) {
  
          $params = array(
            'phone' => $account->field_telephone_number['und'][0]['value'],
            'contact_id' => $contact_id,
            'location_type_id' => 'Main', // Other until main form completed
          );
          $response3 = civicrm_phone_create($params);
  
          if (variable_get('civicrm_api_connect_debug', false)) {   
            watchdog('civicrm', '' . print_r($response3, 1) . '');
          }
        }
  
        $edit['field_civi_id']['und'][0]['value'] = $contact_id;
        // Default primary contact to the web administrator
        $edit['field_primary_contact_first_name']['und'][0]['value'] = $account->field_first_name['und'][0]['value'];
        $edit['field_primary_contact_last_name']['und'][0]['value'] = $account->field_last_name['und'][0]['value'];
  
  This is an update:
  I have added the code for civi_create_contact as requested
  function civicrm_contact_create($params) {
    return civicrm_api_query('Contact', 'create', $params);
  }
  
  And this is the code for civicrm_api_query:
  function civicrm_api_query($entity, $action, $data = array()) {
        $url = civicrm_api_connect_get_base_url() . "/sites/all/modules/civicrm/extern/rest.php";
  
    $keys = civicrm_api_connect_auth();
  
    $params = array(
      'entity' => $entity,
      'action' => $action,
      'api_key' => $keys['apikey'],
      'key' => $keys['sitekey'],
      'json' => 1,
    );
  
    $headers = array(
      'Content-Type' => 'application/x-www-form-urlencoded'
    );
  
    // Send the HTTP basic authentication headers if we are in test mode.
    if (variable_get('civicrm_api_connect_test', false)) {
        $headers['Authorization'] = 'Basic ' .
          base64_encode(
            variable_get('civicrm_api_connect_access_username') . ':' .
            /*decrypt*/(variable_get('civicrm_api_connect_access_password')));
    }
  
    $params = array_merge($params, $data);
  
    $query = drupal_http_build_query($params, '', '&');
  
    if ($action == 'get') {
      $url .= '?' . $query;
      $method = 'GET';
    }
  
    if ($action == 'create') {
      $method = 'POST';
    }
  
    $data = array(
      'method' => $method,
      'headers' => $headers,
      'data' => $query,
    );
  
    // Trace the submitted data if we're in debug mode
    if (variable_get('civicrm_api_connect_debug', false)) {
      watchdog('civicrm', '' . print_r($data, 1) . '');
    }
  
    $response = drupal_http_request($url, $data);
  
    // Trace the received data if we're in debug mode
    if (variable_get('civicrm_api_connect_debug', false)) {
      watchdog('civicrm', '' . print_r($response, 1) . '');
    }
  
    if ($response->code == 200) {
      $data = json_decode($response->data);
  
      // Trace the response if it's an error and we haven't already traced
      // it through being in debug mode.
      if ($data->is_error) {
        drupal_set_message(t('CiviCRM error @error', array('@error' => $data->error_message)), 'error');
  
        if (variable_get('civicrm_api_connect_debug', false)) {
          watchdog('civicrm_error', $data->error_message);
          //watchdog('civicrm_error', '' . print_r($params,1) . '');
        }
        return false;
      }
      else {
        return $data;
      }
    }
    else {
      watchdog('civicrm_error', 'CiviCRM fatal error');
      return false;
    }
  }
  
  **Tags:** civicrm.settings.php
  
  > **Comments:**
  > **username_1**: goulding, welcome to the stack! Your post's subject should ideally be phrased as a question. Please rephrase it?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2021-02-10. Score: 1
  
  Welcome to SE! It looks like you are trying to create an Individual and that your API call doesn't have all the necessary data.
  
  One option is to print out the API parameters just before you make the API call and check it has what you expect. See <https://docs.civicrm.org/dev/en/latest/tools/debugging/#printing-php-variables>
  
  If you edit the question and include your code, or a link to it, then we can offer more specific suggestions.
  
  > **Comments:**
  > **username_0**: Thanks for your help. I will get back to with the code example and edit my question. However I need to wait for my client to refresh the test version of CIVI and make a new test version as an exact copy from live so I can recreate the bug and test.  
  > **username_0**: I have added some code. Although i wont be able to debug as i have said this only happens on live CIVI so i need to refresh our test version. I will let you know when this happens  
  > **username_3**: Could you please clarify the context in which you are running this code, and where the function civicrm_contact_create is defined? That function does not exist in modern versions of CiviCRM.  
  > **username_2**: Thanks for adding the code. Depending how you are running it, I'd add `print_r($account)` at the top since I suspect that does not contain what you expect - in which case the question moves further back to how `$account` is being set.  
  > **username_0**: Hi @username_2 account is defined by Drupal it is an abject for the users signed up to drupal and is part of the hook.   https://api.drupal.org/api/drupal/modules!user!user.api.php/function/hook_user_presave/7.x  It is passed to this hook, we can add additional fields to the user object. Hope that makes sense?  
  > **username_0**: Hi I have now figured out that my statement that this only happens on the live instance of CIVI. Sorry for confusion it happens when debug mode is on we define debug mode in the admin by ticking a box. This is so we can send debug info to the log. for example  if (variable_get('civicrm_api_connect_debug', false)) {     watchdog('civicrm', '<pre>' . print_r($response, 1) . '</pre>');   }  It happens when debug is off. So will try to figure out how this can have an effect on that.  
  
  ---
  
  