How to add a record using JSON with Custom Fields ?

155 views
Skip to first unread message

Ankhaa STG

unread,
Mar 17, 2016, 5:34:55 AM3/17/16
to Capsule API
Hello,

I'm using this library PHP Example .
http://developer.capsulecrm.com/v1/libraries/

Adding record is OK. No problem.

I want to add record with custom fields. How do i do ?

Please answer my question.
Thank you.

Michael Josephson

unread,
Mar 17, 2016, 5:38:40 AM3/17/16
to Capsule API
Hi there!

You need to  make a separate API call to add the custom field values. So basic approach would be:

- Add the person/organisation as in the example
- Check the Location header on the response to obtain the ID of the new record

Cheers,

-Michael

Carlo Moriones

unread,
Nov 27, 2017, 5:56:07 AM11/27/17
to Capsule API
How to do this on v2?

Michael Josephson

unread,
Nov 27, 2017, 6:05:20 AM11/27/17
to Capsule API
Hi Carlo!

In API v2 there is no longer a need to make a separate request. Custom fields can be included in the JSON like this:

{
  "party": {
    "type": "person",
    "firstName": "New",
    "lastName": "Person",
    "fields": [
      { "definition": { "id": 123 }, "value": "field value" }
    ]
  }
}

or using a shorthand for the definition ID like this: 

"fields": [
  { "definition": 123, "value": "field value" }
]

The "List custom fields" endpoint can be used to find the definition ID for the custom field, like this: https://developer.capsulecrm.com/v2/operations/Custom_Field#listFields

Cheers,

-Michael

Carlo Moriones

unread,
Nov 28, 2017, 4:02:37 AM11/28/17
to Capsule API
Didnt worked for me tho.

This is my code

$contact_id = "158176876";

$inputfields = array(
        'party' => array(
        'fields' => array(
          'definition' => 450793,
          'value' => '123456'
      )

        )
        );

$json_fields = $json = json_encode($inputfields);


$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://api.capsulecrm.com/api/v2/parties/".$contact_id);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_POSTFIELDS, "{\n  \"party\": {\n    \"type\": \"organisation\",\n    \"name\": \"Acme\"\n  }\n}");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_fields);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");


$headers = array();
$headers[] = "Authorization: Bearer {bearer}";
$headers[] = "Content-Type: application/json";
$headers[] = "Accept: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
echo "Hello World!";
curl_close ($ch);


?>

Michael Josephson

unread,
Nov 28, 2017, 4:24:39 AM11/28/17
to Capsule API
Hey again Carlos!

That code is really close. Because it's possible to set several custom fields, "fields" in the JSON is an array rather than a single object. This means you need one more array() around the field, like this:

...
'party' => array(
  'fields' => array(array('definition' => 450793, 'value' => '123456'))
)
...

A good way to troubleshoot issues like this is to use the pretty printing in PHP. If you add JSON_PRETTY_PRINT to your json_encode:

$json_fields = $json = json_encode($inputfields, JSON_PRETTY_PRINT);

and then print out your $json_fields you can check that the structure of the JSON you've built matches the example.

Cheers!

-Michael

Carlo Moriones

unread,
Nov 28, 2017, 6:24:56 AM11/28/17
to Capsule API
Hello Michael,

I changed my array but still It didnt worked. Heres my new array


$inputfields = array(
        'party' => array(
        'type' => 'organisation',
        'name' => 'DEF INC',
        'fields' => array(
          array('definition' => array(
          'id' => 450793,
          'value' => '123456'
             )
         
      )
          )
        )
        );

$json_fields = $json = json_encode($inputfields);




Regards,
Carlo

Michael Josephson

unread,
Nov 28, 2017, 6:37:26 AM11/28/17
to Capsule API
Hi!
 
I changed my array but still It didnt worked. Heres my new array

The JSON for the fields there looks good so it could be something specific to what you're doing with the organisation, or something related to the type of your custom field.

I'll pick this up with you via our support help desk so that we can go into more detail.

Cheers!

-Michael

Michael Josephson

unread,
Nov 28, 2017, 6:52:02 AM11/28/17
to Capsule API
Hi!

I just took another look at your new array with one of the other developers on the team. It looks like the value is incorrectly nested inside with the definition. As mentioned, let me pick this up with you via our support. Speak soon!

-Michael

Rajveer singh jhala

unread,
Nov 21, 2018, 4:14:36 AM11/21/18
to Capsule API
Hello Michael,

Can you please tell us the json form to post custom field to CASE in V2 API

Mike Smith

unread,
Nov 21, 2018, 4:19:45 AM11/21/18
to Capsule API
Hi Rajveer,

You can use an approach quite similar to the parties JSON to add a custom field to a case.

For example :

{
  "kase": {
    "fields": [
      {
        "definition": {
          "id": 7
        },
        "value": "field value"
      }
    ]
  }
}

Or you could use the shorthand version:

{
  "kase": {
    "fields": [
      {
        "definition": 7,
        "value": "field value"
      }
    ]
  }
}

Thanks,
Mike

Rajveer singh jhala

unread,
Nov 26, 2018, 4:26:26 AM11/26/18
to Capsule API
Thanks for your quick reply ,

I have used format as suggested by you and now it is working like a charm.

I have few suggestion for you guys:

1) There should be a reference for adding custom field on CASE on Documentations
2) On this page https://xxxxx.capsulecrm.com/settings/customfields/party there should be id of custom fields need to be present.

Thanks once again


Reply all
Reply to author
Forward
0 new messages