The Community is hosting an End of Summer sweepstakes! Participants must complete tasks to earn tickets that will enter them with a chance to win a free year of Constant Contact and other great prizes!*
*No Purchase Necessary. For Official Rules, visit here. Constant Contact’s End of Summer 2020 Sweepstakes ends on October, 20, 2020 at 11:50 PM EST.

Bulk Activities - Add contacts - Need Help

SOLVED
Go to solution
Highlighted
Occasional Contributor

Bulk Activities - Add contacts - Need Help

Hello Support,

 

Following is the Java code that I have written for uploading contacts in bulk using the most recent version of Java SDK.

I get the following errors when I try to execute the code:

 

 

import com.constantcontact.ConstantContact;
import com.constantcontact.components.activities.contacts.request.ContactData;
import com.constantcontact.components.activities.contacts.request.CustomField;
import com.constantcontact.components.activities.contacts.request.AddContactsRequest;
import com.constantcontact.components.contacts.ContactList;
import com.constantcontact.exceptions.component.ConstantContactComponentException;
import com.constantcontact.exceptions.service.ConstantContactServiceException;

 

import java.util.ArrayList;
import java.util.List;

 

public class BulkMemberUpload
{
public BulkMemberUpload()
{

}

 

public static void UploadMembers(ArrayList alMemberId, ArrayList alFirstName,
ArrayList alLastName, ArrayList alEmailId,
String strAPIKey, String strAccessToken)
{
// Create Constant Contact object
ConstantContact objConstantContact = new ConstantContact(strAPIKey, strAccessToken);

// Create AddContactsRequest object
AddContactsRequest objAddContactsRequest = new AddContactsRequest();

// Create new list of contact data
List<ContactData> lsContactData = new ArrayList<ContactData>();

// Create list of columns
List<String> lsColumnName = new ArrayList<String>();

// Create object for contact list
List<String> contactlist = new ArrayList<String>();

// ContactList object
ContactList objContactList = new ContactList();

for(int i=0; i < alMemberId.size(); i++)
{
// Create new contact data object
ContactData objContactData = new ContactData();

// Set Email Id
List<String> lsEmailAddresses = new ArrayList<String>();
lsEmailAddresses.add(alEmailId.get(i).toString());
objContactData.setEmailAddresses(lsEmailAddresses);

// Set First Name
objContactData.setFirstName(alFirstName.get(i).toString());

// Set Last Name
objContactData.setLastName(alLastName.get(i).toString());

// Set Member Id
CustomField objCustomField = new CustomField();
List<CustomField> lsCustomField = new ArrayList<CustomField>();
objCustomField.setName("MemberId");
objCustomField.setValue(alMemberId.get(i).toString());
lsCustomField.add(objCustomField);
objContactData.setCustomFields(lsCustomField);

 

// Add contact to contact list
lsContactData.add(objContactData);

} // End of for


// Add column names for list
lsColumnName.add("EMAIL");
lsColumnName.add("FIRST NAME");
lsColumnName.add("LAST NAME");
lsColumnName.add("CUSTOM FIELD 1");

// Set List id
contactlist.add("4");

 

// Set list name for upload
objAddContactsRequest.setLists(contactlist);

// Set column names
objAddContactsRequest.setColumnNames(lsColumnName);

// Import data into AddContactsRequest
objAddContactsRequest.setImportData(lsContactData);


// Upload members
try {
System.out.println(objAddContactsRequest.toJSON());
objConstantContact.addBulkContacts(objAddContactsRequest);
} catch (ConstantContactServiceException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
System.out.println("Caught in Exception while uploading the list " + e.getErrorInfo());
} catch (ConstantContactComponentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

 

public static void main (String[] args)
{
String strAPIKey = "Your_API_Key";
String strAccessToken = "Your_Access_Token";

ArrayList alMemberId = new ArrayList();
ArrayList alFirstName = new ArrayList();
ArrayList alLastName = new ArrayList();
ArrayList alEmailId = new ArrayList();

alMemberId.add("0001");
alFirstName.add("ABCD");
alLastName.add("EFGH");
alEmailId.add("abcdefgh@gmail.com");

alMemberId.add("0002");
alFirstName.add("PQRS");
alLastName.add("TUVW");
alEmailId.add("pwer238@yahoo.com");

 

alMemberId.add("0003");
alFirstName.add("PERIWE");
alLastName.add("SDFKSD");
alEmailId.add("wierj@hotmail.com");

UploadMembers(alMemberId, alFirstName, alLastName, alEmailId, strAPIKey, strAccessToken);

}

 

}

 

 

Caught in Exception while uploading the list [
CUrlRequestError [errorKey=json.field.invalid, errorMessage=#/import_data/0/custom_fields: Property was found but is not permitted at this location.],
CUrlRequestError [errorKey=json.field.invalid, errorMessage=#/import_data/1/custom_fields: Property was found but is not permitted at this location.],
CUrlRequestError [errorKey=json.field.invalid, errorMessage=#/import_data/2/custom_fields: Property was found but is not permitted at this location.],
CUrlRequestError [errorKey=url, errorMessage=https://api.constantcontact.com/v2/activities/addcontacts]]

 

Can you please look into my code and let me know what is the cause for the errors mentioned above?  Please replace the strings "Your_API_Key" and "Your_Access_Token" in the main() method with the actual values.  I have removed the actual values from my program for security reasons.

 

Thanks,

Srinivas

 

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Regular Advisor

Re: Bulk Activities - Add contacts - Need Help

Hi there,

 

If you're looking to use multiple custom fields,  they both need to be added to the list before being set in the ContactData object.  This should do the trick:

 

// Set Member Id
            CustomField objCustomField = new CustomField();
            List<CustomField> lsCustomField = new ArrayList<CustomField>();
            objCustomField.setName("CUSTOM FIELD 1");
            objCustomField.setValue(alMemberId.get(i).toString());
            lsCustomField.add(objCustomField);
            //objContactData.setCustomFields(lsCustomField);

            CustomField objCustomField2 = new CustomField();
            //List<CustomField> lsCustomField = new ArrayList<CustomField>();
            objCustomField2.setName("CUSTOM FIELD 2");
            objCustomField2.setValue(alMemberId.get(i).toString());
            lsCustomField.add(objCustomField2);
            objContactData.setCustomFields(lsCustomField);

// Add contact to contact list
            lsContactData.add(objContactData);

 

View solution in original post

27 REPLIES 27
Highlighted
Moderator

Re: Bulk Activities - Add contacts - Need Help

Sorry for the problem you are running into.  Our development team is looking into this right now and we hope to have a fix out for this very soon.  The problem is not your code, it's a bug in our JSON Schema validation.  We should have a fix for this by mid week and are working on this as a top priority.  Thanks for reporting!

Dave Berard
Senior Product Manager, Constant Contact
Highlighted
Occasional Contributor

Re: Bulk Activities - Add contacts - Need Help

Thanks, I will be looking for the fix coming ahead...

Highlighted
Moderator

Re: Bulk Activities - Add contacts - Need Help

We have tested a fix in our development environment and have it planned to be released Thurs morning.  Will update once we confirm we've released the fix in production.  Sorry again for the frustration here!

Dave Berard
Senior Product Manager, Constant Contact
Highlighted
Occasional Participant

Re: Bulk Activities - Add contacts - Need Help

I'm still seeing an error with this when using the phpWrapper library.  Calling the addCreateContactsActivity, with contacts that have a custom field returns an error saying.

 

[{"error_key":"json.regex.mismatch","error_message":"#/import_data/0/custom_fields/0/name: Value does not match the required regex pattern: ^Custom\\s?Field\\s?1[0-5]$|^Custom\\s?Field\\s?[1-9]$."}]

 

Looking at the AddContacs class the expected name value for custom fields is in the format custom_field_[0-15].  The library appears to update this value for the columns array in the requwt to the format "CUSTOM FIELD [0-15]" but does not ever update the name attribute in the contact->custom field array to the expected key value for the server.

 

As a quick work around I modified the toJson function to update the name 

 

public function toJson()
{
    foreach ($this->import_data as $contact) {
        if( !empty( $contact->custom_fields ) ){
            foreach( $contact->custom_fields as &$cf ){
                $cf->name = Config::get('activities_columns.' . $cf->name );
            }
        }
        foreach ($contact as $key => $value) {
            if ($value == null) {
                unset($contact->$key);
            }
        }
    }
    return json_encode($this);
}

 

running this gets past the server side regx check but generates an error saying 

 

[{"error_key":"http.status.internal_server_error","error_message":"We are currently experiencing an issue with our service, please try again in a few minutes or contact Customer Support at webservices@constantcontact.com."}]

 

When I remove all custom field information the bulk add works correctly.

 

Here is the json data that generates the regx exception with no modifications to the toJson function

 

{"import_data":[{"first_name":"John","last_name":"Doe","email_addresses":["john.doe@hsc.edu"],"custom_fields":[{"name":"custom_field_1","value":"325"}]},{"first_name":"Jane","last_name":"Doe","email_addresses":["jane.doe@test.com"],"custom_fields":[{"name":"custom_field_1","value":"1037"}]}],"lists":["1"],"column_names":["EMAIL","FIRST NAME","LAST NAME","CUSTOM FIELD 1"]}

 

 

Here is the modifed json that generates the internal server error

 

{"import_data":[{"first_name":"John","last_name":"Doe","email_addresses":["john.doe@hsc.edu"],"custom_fields":[{"name":"CUSTOM FIELD 1","value":"325"}]},{"first_name":"Jane","last_name":"Doe","email_addresses":["jane.doe@test.com"],"custom_fields":[{"name":"CUSTOM FIELD 1","value":"1037"}]}],"lists":["1"],"column_names":["EMAIL","FIRST NAME","LAST NAME","CUSTOM FIELD 1"]}

Highlighted
Moderator

Re: Bulk Activities - Add contacts - Need Help

Thanks for sending this over.  We did release a fix yesterday afternoon for the custom field errors and confirmed that is all set.  We'll take a look at this new report and let you know when we have more information.

Dave Berard
Senior Product Manager, Constant Contact
Highlighted
Occasional Contributor

Re: Bulk Activities - Add contacts - Need Help

Hi Dave,

 

Thanks for the confirmation.  However I am seeing the following error message now when I try to compile the Java code posted earlier (the very first program in this thread).

 

CUrlRequestError [errorKey=json.regex.mismatch, errorMessage=#/import_data/0/custom_fields/0/name: Value does not match the required regex pattern: ^Custom\s?Field\s?1[0-5]$|^Custom\s?Field\s?[1-9]$.]

 

Could you please explain me about this error and how to fix it? The other day you confirmed that my code was correct, please note that I have not modified anything in my code.

 

Regards,

Srinivas

Highlighted
Employee

Re: Bulk Activities - Add contacts - Need Help

Hi Srinivas,

 

Our developers are working on this, and if all goes well, this custom field issue will be resolved by Friday afternoon.  Thanks for your patience!

 

Best Regards,

Shannon W.

API Support Specialist

Highlighted
Occasional Contributor

Re: Bulk Activities - Add contacts - Need Help

Hi Shannon,


Looks like the bulk member upload API is working fine now.  I am able to upload the members into the account using the code that I have mentioned in the beginning of this thread.  However I am still unable to set the label and the value for the custom field.  Not sure if it is something that I need to fix in my code.

 

Also, can you please let me know the limit of member upload count for trial account?  Looks like I am able to upload 500 members in one shot into the account.  However I get the invalid json payload error when I try to load more than 7,000 members.  Also, please note that I am able to upload 7,000+ members manually into the account from the CSV file but not from my code.  My code uploads successfully when the load is fed in batches of 500.

 

Thanks,

Srinivas

 

 

Highlighted
Moderator

Re: Bulk Activities - Add contacts - Need Help

We have some size limitations for the bulk imports from the API (which are identical to the UI limitations, just exposed in different ways).  You can read them all here, but here is an except that should answer your questions:

 

Limitations

The size of the JSON request payload must be less than 4 megabytes. Also, the  number of contacts that you can import in a single POST is limited to 20,000. The activity request will fail if the payload is greater than 4 MBs or if there are more 20,000 contacts. Remember, the more columns or properties that you include with the imported contact, the bigger the JSON payload will be

Dave Berard
Senior Product Manager, Constant Contact
Developer Portal

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

Visit Page

Constant Contact 2020 End of Summer Community Sweepstakes!

The Constant Contact User Community is hosting a sweepstakes. The more you participate, the more chances you have to win! Read on to learn more...

Read More
Featured