cancel
Showing results for 
Search instead for 
Did you mean: 
Constant Contact wants to help you succeed! We’re celebrating our professional service programs on the Constant Contact Community this month and you have a chance to try one of the services for free! Learn more.

API Add Contact with Custom Fields - Account Number

SOLVED
Occasional Contributor

API Add Contact with Custom Fields - Account Number

Hello,

 

I am trying to upload a contact via the API and I'd like to also add their Account Number during the upload of the contact.  Here is what I've tried with no success:

 

$custF = [];
$custF = ['account_number' => '12345'];

$contact = new Contact();
$contact->addEmail($_POST['email']);
$contact->addList('1285823438');
$contact->addList('1597339570');
$contact->addList('1717557123');
$contact->first_name = $_POST['first_name'];
$contact->last_name = $_POST['last_name'];
$contact->custom_fields($custF);

 

I thought I read it needs to be in an array but that doesn't seem to work. I also tried:

 

$contact->custom_fields->account_number('12345');

and

$contact->account_number('12345');

 

I cannot find any documentation on this as well.

I appreciate any insight.

Tags (3)
6 REPLIES 6
Moderator

Re: API Add Contact with Custom Fields - Account Number

Hello @BarryB854,

 

Thank you for reaching out to Constant Contact's API Support.

 

The custom field does need to be in an array. Here is an example of the JSON.

"custom_fields": [
        {
            "name": "CustomField1",
            "value": "Has control of $25 million budget"
        }
    ]

You can find this in our documentation under the Structure section.

https://developer.constantcontact.com/docs/contacts-api/contacts-collection.html?method=POST

 

Please keep in mind that your custom field names need to use the correct naming convention for the v2 API which is listed in the documentation.


Regards,
Jimmy D.
Tier II API Support Engineer
Occasional Contributor

Re: API Add Contact with Custom Fields - Account Number

Jimmy_D thanks for the response....

 

Would the Keys of the JSON code be the exact name I named my custom fields?  for example:

"custom_fields":[{"name": "Account Number", "value" : "123412"}] 

Occasional Contributor

Re: API Add Contact with Custom Fields - Account Number

I've read the documentation and created: CustomField1.

 

Then I tried the following JSON arrays. I used your one example and I also tried json_encode(); via PHP.

 

PHP CODE:

$custFPre = ['custom_fields' => ['name' => 'CustomField1', 'value' => '12345']];
$custF = json_encode($custFPre);

JSON From Above:

{"custom_fields":{"name":"CustomField1","value":"12345"}}

Here is what your example I used:

$custF = '"custom_fields": [
                        {
                            "name": "CustomField1",
                            "value": "343465"
                        }
                    ]';

Then my actual PHP calling the contact() object:

$contact = new Contact();
            $contact->addEmail($_POST['email']);
            $contact->addList('1285823438');
            $contact->addList('1597339570');
            $contact->addList('1717557123');
            $contact->first_name = $_POST['first_name'];
            $contact->last_name = $_POST['last_name'];
            $contact->custom_fields = $custF;

This is the error I receive:

Array
(
    [0] => stdClass Object
        (
            [error_key] => json.type.invalid
            [error_message] => #/custom_fields: Value is of a disallowed type. Allowed types are: Array, Null.
        )

)

I appreciate any insight.

Moderator

Re: API Add Contact with Custom Fields - Account Number

Hello,

 

It looks like you're using our SDK, so you don't need to JSON encode your content.

 

You can pass your custom field in by creating a custom field component, setting the values, and then adding it to the contact object.

$customField = new CustomField();
$customField->name = "CustomField1";
$customField->value = "343465";
$contact->addCustomField($customField);

 

Please give this a try and let me know if you're still seeing issues.


Regards,
David B.
Tier II API Support Engineer

Occasional Contributor

Re: API Add Contact with Custom Fields - Account Number

That did the trick.


Will that ever be updated with you can use your own naming convention instead of the custom_field_n?

 

Thanks!

Highlighted
Moderator

Re: API Add Contact with Custom Fields - Account Number

Hello,

 

All of our SDK libraries only work in our V2 API. The V2 API can only see and use custom fields that follow a very specific naming convention (such as "Custom Field 1" for example) as it was designed with our older contacts system which did not have support for named custom fields. While it is possible to create custom fields with other naming formats using our website, the V2 API cannot see or interact with custom fields outside of that convention.

 

Our new V3 API does have the ability to work with named custom fields, however we don't have any SDK libraries available for use with our V3 API. But if you are interested in taking a look, you can find the documentation for V3 here:
https://v3.developer.constantcontact.com/index.html

 

Regards,
David B.
Tier II API Support Engineer