Answers.Body: <p>To try and reproduce this, I visited the CiviCRM.org demo site at <a href="https://dmaster.demo.civicrm.org/" rel="nofollow noreferrer">https://dmaster.demo.civicrm.org/</a>, logged in, and explored the <a href="https://dmaster.demo.civicrm.org/civicrm/api#explorer" rel="nofollow noreferrer">API Explorer there</a>. You might prefer to use a <a href="https://civicrm.org/demo" rel="nofollow noreferrer">demo site</a> that matches your platform / version (&amp; note that some folk have found the CiviCRM.org hosted demo sites can be hard to spot beneath the partner-hosted sites on that page).</p>
  
  <p>I found a contact via the regular CiviCRM UI and edited their "Marital Status" and "Most Important Issue" constituent info on the contact edit screen, then used the API explorer to <em>return</em> the custom data which had been set.</p>
  
  <p><a href="https://i.sstatic.net/Aoerq.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Aoerq.png" alt="enter image description here"></a>
  <a href="https://i.sstatic.net/eYb6D.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/eYb6D.png" alt="enter image description here"></a></p>
  
  <p>I then used the same process to try and update the custom data, setting "Marital Status" to "M" (the API explorer presents available options in a list).</p>
  
  <p><a href="https://i.sstatic.net/a1R2U.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/a1R2U.png" alt="enter image description here"></a></p>
  
  <p>This showed a successful result and I confirmed that the update worked by viewing the contact record and seeing that "Other" had now changed to "Married". This indicates that the CiviCRM.org demo site works for this use case (suggesting that the issue you're experiencing might be configuration or permission-dependent, or related to some other site-specific implementation detail).</p>
  
  <p>By observing the network tab while testing these API calls, I could observe the response from CiviCRM. In the event where an API call via JS doesn't return a result, I'd look here to see if there was an unexpected response (either invalid JSON, which could possibly leave CiviCRM's JS API hung, or an HTTP response code other than 200, which might have similar results).</p>
  
  <p><a href="https://i.sstatic.net/Ne2z7.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Ne2z7.png" alt="enter image description here"></a></p>
  
  <p>I see your posted code is PHP not JS, so it may be you aren't using the JS API and can discount the HTTP layer as causing your issues. In that case your next step is probably to inspect the result of the attempted API call, and to investigate error logging (also valid when you are using CiviCRM's HTTP/JS interface). </p>
  
  <p>In that case, inspecting details recorded to the CiviCRM debug log, and your hosting environment's PHP and webserver logs can reveal if there's any indication of an issue there. <a href="https://civicrm.stackexchange.com/questions/6399/how-do-i-debug-civicrm-when-i-hit-a-blank-page-white-screen-fatal-error">This question</a> has a detailed writeup on where to find the debug log and other logs, as does the <a href="https://docs.civicrm.org/dev/en/latest/tools/debugging/#viewing-log-files" rel="nofollow noreferrer">CiviCRM docs</a>.</p>
  
  <p>There's a related question on <a href="https://civicrm.stackexchange.com/questions/103/what-is-the-correct-way-to-work-with-custom-data-in-the-api">setting custom data via API</a> which suggests that custom data is not consistently supported by various entity types when modified using the API. That's an old question and might not be up to date.</p>
  
  <p>Hope this is some help!</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-05-14T20:21:27.603Z
Answers.Text: Very helpful, thank you! So looking in the JS Console I'm seeing this error: `{"IP":"97.118.138.254","level":"security","referer":"","reason":"CSRF suspected","is_error":1,"error_message":"SECURITY ALERT: Ajax requests can only be issued by javascript clients, eg. CRM.api3()."}` Why would it be erroring out on a Cross-Site Request Forgery for custom fields and not standard fields?
Answers.UserDisplayName: Jason Bertolacci
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-05-14T20:59:38.373Z
Answers.Text: Looks like you're hitting [this code here](https://github.com/civicrm/civicrm-core/blob/358a1864ba3c5a3fc3ff65f03b3db045ea2199a8/CRM/Utils/REST.php#L442). I'm not sure why you would *not* be seeing that header on an API request initiated by `CRM.api3`, it may be that your webserver doesn't pass the HTTP header `X-Requested-With` through in `$_SERVER['HTTP_X_REQUESTED_WITH']`? Perhaps CiviCRM errs in assuming that all PHP configurations will carry that header across, I'm not sure.
Answers.UserDisplayName: Chris Burgess
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-05-14T21:01:17.243Z
Answers.Text: But: If other `CRM.api3` calls do work, and this issue is specific to those setting fields via `CRM.api3('Contact.create', ['id' => 123, 'custom_1' => 123])`, that's peculiar as the code linked in previous comment shouldn't be specific to custom fields. _(API code here not tested, just for discussion purposes!)_
Answers.UserDisplayName: Chris Burgess
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-05-14T18:53:40.437Z
Answers.LastActivityDate: 2019-05-14T18:53:40.437Z
Answers.UserDisplayName: Chris Burgess
Body: <p>I'm trying to troubleshoot an issue I'm having with api v3. I can successfully use the api to modify standard CiviCRM contact fields, but if I use the api to modify a contact's custom field the call never completes. </p>
  
  <p>In the api explorer I can create a call to modify a custom field on a contact and click "execute", but nothing is returned – no response or error, the icon in the results box just continues to spin. I'm not seeing an error in the log file either. </p>
  
  <p>So this api call does not execute:</p>
  
  <pre><code>$result = civicrm_api3('Contact', 'create', [
    'debug' =&gt; 1,
    'id' =&gt; 4,
    'custom_6' =&gt; 1,
  ]);
  </code></pre>
  
  <p>And this one executes successfully:</p>
  
  <pre><code>$result = civicrm_api3('Contact', 'create', [
    'debug' =&gt; 1,
    'id' =&gt; 4,
    'do_not_email' =&gt; 1,
  ]);
  </code></pre>
  
  <p>I feel like I am missing something obvious like a wrong permission, but would it return an error on that? Any ideas of where to look to troubleshoot? </p>
  
Tags: <contacts><custom-fields><api>
Title: API call to modify contact custom data fields fails without error
ThreadText: # API call to modify contact custom data fields fails without error
  
  (Asked by: username_0 on 2019-05-14)
  
  I'm trying to troubleshoot an issue I'm having with api v3. I can successfully use the api to modify standard CiviCRM contact fields, but if I use the api to modify a contact's custom field the call never completes. 
  In the api explorer I can create a call to modify a custom field on a contact and click "execute", but nothing is returned – no response or error, the icon in the results box just continues to spin. I'm not seeing an error in the log file either. 
  So this api call does not execute:
  $result = civicrm_api3('Contact', 'create', [
    'debug' => 1,
    'id' => 4,
    'custom_6' => 1,
  ]);
  
  And this one executes successfully:
  $result = civicrm_api3('Contact', 'create', [
    'debug' => 1,
    'id' => 4,
    'do_not_email' => 1,
  ]);
  
  I feel like I am missing something obvious like a wrong permission, but would it return an error on that? Any ideas of where to look to troubleshoot?
  
  **Tags:** contacts, custom-fields, api
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-05-14. Score: 1
  
  To try and reproduce this, I visited the CiviCRM.org demo site at <https://dmaster.demo.civicrm.org/>, logged in, and explored the [API Explorer there](https://dmaster.demo.civicrm.org/civicrm/api#explorer). You might prefer to use a [demo site](https://civicrm.org/demo) that matches your platform / version (& note that some folk have found the CiviCRM.org hosted demo sites can be hard to spot beneath the partner-hosted sites on that page).
  
  I found a contact via the regular CiviCRM UI and edited their "Marital Status" and "Most Important Issue" constituent info on the contact edit screen, then used the API explorer to *return* the custom data which had been set.
  
  [![enter image description here](https://i.sstatic.net/Aoerq.png)](https://i.sstatic.net/Aoerq.png)
  [![enter image description here](https://i.sstatic.net/eYb6D.png)](https://i.sstatic.net/eYb6D.png)
  
  I then used the same process to try and update the custom data, setting "Marital Status" to "M" (the API explorer presents available options in a list).
  
  [![enter image description here](https://i.sstatic.net/a1R2U.png)](https://i.sstatic.net/a1R2U.png)
  
  This showed a successful result and I confirmed that the update worked by viewing the contact record and seeing that "Other" had now changed to "Married". This indicates that the CiviCRM.org demo site works for this use case (suggesting that the issue you're experiencing might be configuration or permission-dependent, or related to some other site-specific implementation detail).
  
  By observing the network tab while testing these API calls, I could observe the response from CiviCRM. In the event where an API call via JS doesn't return a result, I'd look here to see if there was an unexpected response (either invalid JSON, which could possibly leave CiviCRM's JS API hung, or an HTTP response code other than 200, which might have similar results).
  
  [![enter image description here](https://i.sstatic.net/Ne2z7.png)](https://i.sstatic.net/Ne2z7.png)
  
  I see your posted code is PHP not JS, so it may be you aren't using the JS API and can discount the HTTP layer as causing your issues. In that case your next step is probably to inspect the result of the attempted API call, and to investigate error logging (also valid when you are using CiviCRM's HTTP/JS interface).
  
  In that case, inspecting details recorded to the CiviCRM debug log, and your hosting environment's PHP and webserver logs can reveal if there's any indication of an issue there. [This question](https://civicrm.stackexchange.com/questions/6399/how-do-i-debug-civicrm-when-i-hit-a-blank-page-white-screen-fatal-error) has a detailed writeup on where to find the debug log and other logs, as does the [CiviCRM docs](https://docs.civicrm.org/dev/en/latest/tools/debugging/#viewing-log-files).
  
  There's a related question on [setting custom data via API](https://civicrm.stackexchange.com/questions/103/what-is-the-correct-way-to-work-with-custom-data-in-the-api) which suggests that custom data is not consistently supported by various entity types when modified using the API. That's an old question and might not be up to date.
  
  Hope this is some help!
  
  > **Comments:**
  > **username_0**: Very helpful, thank you! So looking in the JS Console I'm seeing this error: `{"IP":"97.118.138.254","level":"security","referer":"","reason":"CSRF suspected","is_error":1,"error_message":"SECURITY ALERT: Ajax requests can only be issued by javascript clients, eg. CRM.api3()."}` Why would it be erroring out on a Cross-Site Request Forgery for custom fields and not standard fields?  
  > **username_1**: Looks like you're hitting [this code here](https://github.com/civicrm/civicrm-core/blob/358a1864ba3c5a3fc3ff65f03b3db045ea2199a8/CRM/Utils/REST.php#L442). I'm not sure why you would *not* be seeing that header on an API request initiated by `CRM.api3`, it may be that your webserver doesn't pass the HTTP header `X-Requested-With` through in `$_SERVER['HTTP_X_REQUESTED_WITH']`? Perhaps CiviCRM errs in assuming that all PHP configurations will carry that header across, I'm not sure.  
  > **username_1**: But: If other `CRM.api3` calls do work, and this issue is specific to those setting fields via `CRM.api3('Contact.create', ['id' => 123, 'custom_1' => 123])`, that's peculiar as the code linked in previous comment shouldn't be specific to custom fields. _(API code here not tested, just for discussion purposes!)_  
  
  ---
  
  