3

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!

1 Answer 1

3

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!

3
  • Awesome, thanks! Would you say this is a bug with the api explorer code generation for Javascript?
    – Sleewok
    Sep 1, 2016 at 13:16
  • 2
    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! Sep 1, 2016 at 15:33
  • 3
    More like fixing an existing hack: github.com/civicrm/civicrm-core/pull/8981
    – Coleman
    Sep 4, 2016 at 1:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.