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.

I am unable to get more than 500 conatcts through java API

Highlighted
Occasional Participant

I am unable to get more than 500 conatcts through java API

Hello Team, We are using Constant contact Java API.It is very use full for us and we are very impressed with this Java API.But we are facing small issue while retrieving all contacts. We are unable to get more than 500 contacts using java API.Can you please provide code or process to meet this requirement. Thanks Santosh Kola Email:

Tags (2)
15 REPLIES 15
Highlighted
Honored Contributor

Re: I am unable to get more than 500 conatcts through java API

Hello,

 

In this case what's happening is that since there are more than 500 results, the data that comes back from the API is paginated. You could handle this using the PaginationHelperService class as follows:

 

// Set up the service factory
ConstantContactFactory _serviceFactory = new ConstantContactFactory("ACCESS_TOKEN","API_KEY");

// Create the PaginationHelperService
PaginationHelperService _pagingService =  _serviceFactory.createPaginationHelperService();

// Create the ContactService
IContactService _contactService = _serviceFactory.createContactService();


// Define a place to store results per page, as well as aggregated contacts.
ResultSet<Contact> results;
List<Contact> contacts = new ArrayList<Contact>();

try {
    // Get the first page of contacts.
    results = _contactService.getContacts(500, null, null);

    // Store the first page of contacts.
    contacts.addAll(results.getResults());

    // As long as there continue to be more pages, keep fetching them
    while (results.getMeta().getPagination() != null) {
        results = _pagingService.getPage(results.getMeta().getPagination(), Contact.class);
        // Store the contacts as we fetch them
        if (results.getResults() != null) {
            contacts.addAll(results.getResults());
        }
    }
} catch (Exception e) {
    e.printStackTrace();
}

// Once the loop terminates, the List "contacts" will contain all contacts.

 

If you have any questions, please let me know! As a note, this code may not directly build, as I had to write this at the end of the day and haven't had a chance to properly test it. If there's any issues, I should be available Monday to correct them!

 

EDIT: The code in this post has been updated and tested

 

Sincerely,

Elijah G.
API Support Engineer
Highlighted
Occasional Participant

Re: I am unable to get more than 500 conatcts through java API

Elijah G.

Good Morning,

Thanks for your replay.We have used your given code but getting compile time error in below line.Because of last parameter i.e Contact . Can you please suggest  how that object should be declared and initialized.

 

 results = _pagingService.getPage(results.getMeta().getPagination(),Contact);

 

Thanks,

Santosh Kola.

Highlighted
Honored Contributor

Re: I am unable to get more than 500 conatcts through java API

Hello,

 

I had made a couple of minor mistakes when I built the original code due to the lack of time available to build and test it. I've updated the code in my previous post with code that has been built and tested. Please let me know if you have any trouble with it!

 

Sincerely,

Elijah G.
API Support Engineer
Highlighted
Occasional Participant

Re: I am unable to get more than 500 conatcts through java API

Good Morning Elijah,

Thanks for your help.Now Its working fine and we are getting total contacts.Can you please give me the solution for below query.

 

could you please clarify my doubt regarding deleting contact permanantly from the constant contact account through java api call so that those contact shouldn't come under billing.
Actually we are using


ContactService cs = new ContactService(Api_Key, Access_Tocken);
 cs.deleteContactFromList(contactId, listId);
 
 but the contact is not deleted permanantly only the status changed to Removed .so we are getting problem that "Email is already existed" when we are trying to add new  contact with same email id .

 

Thanks for your excellent support till now.

 

Santosh Kola.

Highlighted
Honored Contributor

Re: I am unable to get more than 500 conatcts through java API

Hello Santosh,

 

The method that you are using to remove the contact from lists and billing is correct. However, it is important to know that even when deleting a contact in our UI, it is considered a soft delete. This is done to allow contacts to be restored and protect our users in case they were to accidentally delete important contact data.

 

When you are working with the API, the best process for adding contacts is to do the following:

 

Check if a contact with the desired email address already exists. You can do this using ContactService.getContactByEmail(String email). If there is a contact already using that email, the resultset will contain a single result.

 

If the contact already exists, simply update the existing contact with your new details and ensure that it is placed on at least one list. This will change the status back to active. Please note that if the contact is unsubscribed, this can only be done by setting the actionByVisitor flag to True, and should only be done when the API action is a direct result of an action by the contact in question (a signup form is a good example). For this action you will use ContactService.updateContact(Contact contact, Boolean actionByVisitor).

 

If the contact does not yet exist, you can create a new contact. Please note that when creating a new contact, a welcome email will only be sent if the actionByVisitor flag is set to True. For this action you will use ContactService.addContact(Contact contact, Boolean actionByVisitor).

 

 

If you have any questions or if I can clarify, please let me know!

 

Sincerely,

Elijah Gaiter

API Support Engineer

Elijah G.
API Support Engineer
Highlighted
Occasional Participant

Re: I am unable to get more than 500 conatcts through java API

Hello Elijah,

It means when we delete a contact through java api using deleteContactFromLists(contact Id) method , it will changed status into REMOVED..
Now my doubt is removed(Status Removed) contacts will comes under billing or not ?

 

Please clarify my doubt.

Thanks You,

Trivani.

Highlighted
Honored Contributor

Re: I am unable to get more than 500 conatcts through java API

Hello Trivani,

 

Any contact with the following status is not billable:

  • REMOVED
  • OPTOUT

 

Please let me know if you have any additional questions that I can help with!

 

Sincerely,

Elijah G.
API Support Engineer
Highlighted
Occasional Participant

Re: I am unable to get more than 500 conatcts through java API

Hello Elijah,

 

Thanks for your support till now.Can you please give me the solution for below query.

 

Can we have chance of adding bulk of contacts to a constant contact list at once ?

 

Actually we are receiving all contacts in result set from database and we are adding to constant contact list one by one from that Result Set  through java api but while adding contact in iteration of result set...

 

i am getting "java.net.SocketTimeoutException: Read timed out" exception in the middle but we want to add all the contacts that are in resul set without any exception.

 

Could you please help me regarding this issue.

 

Thank you,

Triveni.

 

 

Highlighted
Honored Contributor

Re: I am unable to get more than 500 conatcts through java API

Hello,

 

It is absolutely possible to import contacts in bulk. You can find a sample that I created for another developer to demonstrate the process here:

https://community.constantcontact.com/t5/Developer-Support-ask-questions/Sample-Java-Application/m-p...

 

Please let me know if you have any questions!

 

Sincerely,

Elijah G.
API Support Engineer
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