Answers.Body: <p>The syntax is incorrect - and I just confirmed that the syntax generated by the API Explorer for Getvalue is incorrect, so you're not to blame!  </p>
  
  <p>The culprit is this line:</p>
  
  <pre><code>"return": ["custom_102"],
  </code></pre>
  
  <p>In JSON, the square brackets indicate an array.  "return" expects an array for a <code>Get</code> action, but <code>Getvalue</code> accepts only a single value (for the obvious reason).  If you remove the brackets:</p>
  
  <pre><code>CRM.api3('Contact', 'getvalue', {
    "sequential": 1,
    "return": "custom_102", //custom_fieldId
     "id": 2 // contact id
   }).done(function(result) {
     // do something
   });
  </code></pre>
  
  <p>You should be in business!</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-09-01T13:16:18.563Z
Answers.Text: Awesome, thanks! Would you say this is a bug with the api explorer code generation for Javascript?
Answers.UserDisplayName: Sleewok
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-09-01T15:33:57.557Z
Answers.Text: I would - and I even looked at fixing it.  I realized that a) this is due to a slight inconsistency for "getvalue" in APIv3 that I imagine will be fixed in APIv4, and b) fixing it would basically require a specific hack for "return" on "getvalue".  I decided it wasn't worth it.  I'll ping Coleman on it though!
Answers.UserDisplayName: Jon G - Megaphone Tech
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-09-04T01:27:31.367Z
Answers.Text: More like fixing an existing hack:
  https://github.com/civicrm/civicrm-core/pull/8981
Answers.UserDisplayName: Coleman
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-09-01T04:14:34.047Z
Answers.LastActivityDate: 2016-09-01T04:14:34.047Z
Answers.UserDisplayName: Jon G - Megaphone Tech
Body: <p>I have a custom field for contacts that I would like to retrieve the value for using JavaScript. </p>
  
  <p>Using the API explorer I have generated the code that does this. It works PERFECTLY from the API explorer (which uses PHP). </p>
  
  <p>However, when I run the Javascript code in my extension I get an error. Here is the code:</p>
  
  <pre><code>CRM.api3('Contact', 'getvalue', {
    "sequential": 1,
    "return": ["custom_102"], //custom_fieldId
     "id": 2 // contact id
   }).done(function(result) {
     // do something
   });
  </code></pre>
  
  <p>Here is the error:</p>
  
  <pre><code>Object {
      invalid_field: Array[1],
      is_error: 1, 
      error_message: "field Array unset or not existing" } 
      error_message : "field Array unset or not existing"
      invalid_field : Array[1] 
           0 : "custom_102"
  </code></pre>
  
  <p>I am at a complete loss here... and would really appreciate some help!</p>
  
Tags: <custom-fields><api><javascript>
Title: Getting a Custom Field Value on a Contact with Javascript API
ThreadText: # Getting a Custom Field Value on a Contact with Javascript API
  
  (Asked by: username_0 on 2016-08-31)
  
  I have a custom field for contacts that I would like to retrieve the value for using JavaScript. 
  Using the API explorer I have generated the code that does this. It works PERFECTLY from the API explorer (which uses PHP). 
  However, when I run the Javascript code in my extension I get an error. Here is the code:
  CRM.api3('Contact', 'getvalue', {
    "sequential": 1,
    "return": ["custom_102"], //custom_fieldId
     "id": 2 // contact id
   }).done(function(result) {
     // do something
   });
  
  Here is the error:
  Object {
      invalid_field: Array[1],
      is_error: 1, 
      error_message: "field Array unset or not existing" } 
      error_message : "field Array unset or not existing"
      invalid_field : Array[1] 
           0 : "custom_102"
  
  I am at a complete loss here... and would really appreciate some help!
  
  **Tags:** custom-fields, api, javascript
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-09-01. Score: 3 | Accepted answer
  
  The syntax is incorrect - and I just confirmed that the syntax generated by the API Explorer for Getvalue is incorrect, so you're not to blame!  
  The culprit is this line:
  "return": ["custom_102"],
  
  In JSON, the square brackets indicate an array.  "return" expects an array for a Get action, but Getvalue accepts only a single value (for the obvious reason).  If you remove the brackets:
  CRM.api3('Contact', 'getvalue', {
    "sequential": 1,
    "return": "custom_102", //custom_fieldId
     "id": 2 // contact id
   }).done(function(result) {
     // do something
   });
  
  You should be in business!
  
  > **Comments:**
  > **username_0**: Awesome, thanks! Would you say this is a bug with the api explorer code generation for Javascript?  
  > **username_1**: I would - and I even looked at fixing it.  I realized that a) this is due to a slight inconsistency for "getvalue" in APIv3 that I imagine will be fixed in APIv4, and b) fixing it would basically require a specific hack for "return" on "getvalue".  I decided it wasn't worth it.  I'll ping username_2 on it though!  
  > **username_2**: More like fixing an existing hack: https://github.com/civicrm/civicrm-core/pull/8981  
  
  ---
  
  