cancel
Showing results for 
Search instead for 
Did you mean: 

signup code works locally but not on the live server

SOLVED
Occasional Contributor

signup code works locally but not on the live server

We have the following code that when I run it locally it subscribes me properly to the list.  However when I push it live the form processes successfully and I receive no errors that I can tell (nothing in the error logs and no error emails sent to me) but no subscription is created.  Can anyone tell me what might be going wrong or what I should look for?  In the code below, if errMsg is not empty it emails me but again, I'm not receiving any emails, so it appears that no exception has occurred in the try block.  Thanks.

 

try
{
    var ctct = new ConstantContact(ConstantContactConstants.APIKey, ConstantContactConstants.APIToken);
    var existingContact = ctct.GetContacts(form.EmailAddress, 1, default(DateTime?));
    var contact = new CTCT.Components.Contacts.Contact()
    {
        FirstName = form.FirstName,
        LastName = form.LastName
    };
    contact.EmailAddresses.Add(new EmailAddress(form.EmailAddress));
    contact.Lists.Add(new ContactList()
    {
        Id = form.ConstantContactListId.Value.ToString()
    });

    if (existingContact.Results == null || existingContact.Results.Count == 0)
    {
        ctct.AddContact(contact, false);
    }
    else
    {
        contact.Id = existingContact.Results[0].Id;
        ctct.UpdateContact(contact, false);
    }
   
}
catch (Exception exc)
{
    if (!exc.Message.ToLower().Contains("is already subscribed to the list.")) errMsg = "An error occurred while importing to constant contact.  See below. <p>" + exc + "</p>  The following information was submitted.";
}
4 REPLIES 4
Honored Contributor

Re: signup code works locally but not on the live server

Hello,

 

After reviewing your code, it looks like the exception is being swallowed by your code, because your catch statement only takes action of the exception is specifically caused by an already existing contact. If you were to get an exception that was caused by something else, such as an incorrect API Key, that code would swallow the exception. In order to discover the precise reason for the failure, I would suggest writing a much more general statement in your catch block so that it will give you the message for any exception that is thrown rather than only the case where a contact already exists.

 

Additionally, it is important to note that this code actually has the capability to delete portions of a contact's data when updating. this is because the UpdateContact method of our API writes all contact fields to match the exact input. This means that any fields that are blank in the data that is sent will be updated to be blank in Constant Contact. In order to avoid unintentionally overwriting data, you would want to handle things a bit differently.

 

Here's an example of how to update a contact while preserving the existing data&colon;

try
{
    var ctct = new ConstantContact(ConstantContactConstants.APIKey, ConstantContactConstants.APIToken);
    var existingContact = ctct.GetContacts(form.EmailAddress, 1, default(DateTime?));
    var contact = new CTCT.Components.Contacts.Contact()
    if (existingContact.Results == null || existingContact.Results.Count == 0)
    {
        var contact = new CTCT.Components.Contacts.Contact()
        {
            FirstName = form.FirstName,
            LastName = form.LastName
        };
        contact.EmailAddresses.Add(new EmailAddress(form.EmailAddress));
        contact.Lists.Add(new ContactList()
        {
            Id = form.ConstantContactListId.Value.ToString()
        });
        ctct.AddContact(contact, false);
    }
    else
    {
        var contact = existingContact.Results[0];
        contact.FirstName = form.FirstName;
        contact.LastName = form.LastName;
        contact.Lists.Add(new ContactList()
        {
            Id = form.ConstantContactListId.Value.ToString()
        });
        ctct.UpdateContact(contact, false);
    }
}
catch (Exception exc)
{
    if (!exc.Message.ToLower().Contains("is already subscribed to the list.")) errMsg = "An error occurred while importing to constant contact.  See below. <p>" + exc + "</p>  The following information was submitted.";
}

Please let me know if you have any questions!

 

Sincerely,

Elijah G.
API Support Engineer
Highlighted
Occasional Contributor

Re: signup code works locally but not on the live server

Thank you but actually the exception code is the reverse of what you've interpreted it.  The exception error message is set if it is NOT an existing contact.

 

I will make the updates to keep from deleting data and I'll take out the condition around the exception just to be sure that isn't a problem.

Occasional Contributor

Re: signup code works locally but not on the live server

I've removed the condition surrounding my exception handling and resubmited the form.  I did not receive any errors, didn't get any notification that an error occurred but yet I still don't show up in the list I'm trying to subscribe to.

Honored Contributor

Re: signup code works locally but not on the live server

Hello,

 

After a bit more investigation on this, it looks like you might be connecting with a different account(or the connection could be failing) on your live form than with the test form. Because of this, I'm a bit limited in my ability to gather information. Would it be possible for you to email my team with some additional details? The following details would be a great help:

  • Your API Key
  • The username of the account you are connecting to
  • A timestamp for when a contact should have been added to the account

The best email to reach us is webservices@constantcontact.com.

 

If you have any questions, please let me know!

 

Sincerely,

Elijah G.
API Support Engineer