API Add Contact with Custom Fields - Account Number

SOLVED
Highlighted
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)
1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
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

View solution in original post

11 REPLIES 11
Highlighted
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
Highlighted
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"}] 

Highlighted
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.

Highlighted
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

View solution in original post

Highlighted
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

Highlighted
Solution Provider

Re: API Add Contact with Custom Fields - Account Number

The "solution" does not solve the problem.

 

Turns out V3 solves the custom field names...

but V3 is not useful for collecting leads from stable landing pages with signup forms because there are 3 tokens that need to be generated and regenerated every 2-24 hours. When 100-1000's of people are visiting a page "simultaneously" and one is paying for PPC clicks this causes some serious problems as 50% of more of the subscribes are lost, fail, throw errors, take too long to respond, ect...

 

And with V2 well... that SDK showed some promise. (at first)

Turns out naming 10 custom fields "CustomField1" and "CustomField2" ect... defeated the purpose of having custom fields.

V2 is bulky (I believe it could have been written as 1 class in 400 lines instead of 400 files).

 

It's a shame that V2 worked in all other regards...

Highlighted
Moderator

Re: API Add Contact with Custom Fields - Account Number

Hi @ocreations,

 

Our v3 API uses standard oAuth2 protocols and is very easy to implement. You have obviously already read the documentation. Once you go through the initial steps and the Access/Refresh Tokens are generated you can then store those for later use. The Access Token is of course used for making API calls and the Refresh Token is used to go through Step 5 of the oAuth flow when when Access Token expires.

 

Some folks like to automate the refresh process and there are two main ways to do this. One way is to capture the error message that is generated when the Access Token expires and then automatically run through Step 5. The other way is to create a timer that counts the duration of the Access Token. When the timer expires you then go through Step 5.


Regards,
Jimmy D.
Tier II API Support Engineer
Highlighted
Solution Provider

Re: API Add Contact with Custom Fields - Account Number

It is disappointing that Constant contact has created such a complex roadblock for us to use the V3 system...

To be clear, can you confirm if the following statements are true, false or need disambiguation:

 

In V3 of the API,

  • the only way for us to send custom data
  • from custom forms on our end
  • with custom field names into our Constant Contact list is to:

 

  1. Generate an Oauth Token url using the V3 API
  2. Validate that URL with app permissions
  3. Store the response code 
  4. Send that response code back to your system
  5. Get a TOKEN that will expire either in 2 hours, or, 24 hours
  6. Save that token, and the "refresh token" in a database or a file system
  7. when that token inevitably expires send the "refresh token" back to your API
  8. Store both new token and "refresh token"
  9. repeat 3 through 8 every 2 or 24 hours...
  10. and if something go's wrong, then start at #1 again

And potentially do that for every visit for 1000's of visitors to check if the Token has expired, and if so, then do that again, and again, and again...

Just to add emails, some segmentation data and custom fields into our mailing lists?

 

and... there is no SDK or set of code that does this for us, so we have to write that solution from scratch using the online documentation examples.

and... when that is done, we still need to code up the solution to actually manipulate those lists as well, since our implementation of the V2 SDK  that we have in place does not work with V3?

 

Are all of these statements/assumptions true?

Developer Portal

View API documentation, code samples, get your API key.

Visit Page

Register for our "Making Sense of Online Marketing for Retail" Webinar

Join us on Thursday, August 13th at 2pm ET for a free webinar where we'll discuss how to incorporate online marketing into your retail store.

Read More
Featured