Finding, Creating and Updating Contacts

I don’t recall an Infusionsoft API integration that I have developed that didn’t include retrieving (or adding) a contact record and updating some value. In this post we will examine common contact service tasks that include looking up an Infusionsoft contact by email address and if the contact exists then updating a field value, and if the contact does not exist then adding a new record with the specified field values.

Step One: Create our Infusionsoft API Proxy

[sourcecode language=”c”]
public interface IContactService : IXmlRpcProxy
{
[XmlRpcMethod("ContactService.findByEmail")]
XmlRpcStruct[] FindByEmail(string key, string email, string[] selectedFields);

[XmlRpcMethod("ContactService.load")]
XmlRpcStruct Load(string key, int id, string[] selectedFields);

[XmlRpcMethod("ContactService.add")]
int Add(string key, XmlRpcStruct dataFields);

[XmlRpcMethod("ContactService.update")]
int Update(string key, int contactId, XmlRpcStruct dataFields);
}

[/sourcecode]

FindByEmail Method

Our FindByEmail method will search the contact records to find all contacts with the specified email address. This method is effective at finding contacts when you don’t have the specific contact record id as a reference.

Infusionsoft contacts have three email fields (Email, EmailAddress2, EmailAddress3) and each of those fields will be searched by the FindByEmail method.
* Note The FindByEmail method will NOT search through any custom contact fields in your application.)

Parameters
  • key (string) – The Infusionsoft API Key
  • email (string) – The email address to find
  • selectedFields (string[]) – The contact fields to return in the results
Return Values

This method returns an array of XmlRpcStructs.

*Note Each of the XmlRpcStruct objects contains data for a unique contact record. Since an email address can be assigned to more than one contact in the application, then all contacts will be returned by the FindByEmail method.

Exceptions

XmlRpcFaultException Fault Code 10 If the Infusionsoft API cannot find a supplied contact field, then this exception will be thrown with the message “Server returned a fault exception: [10] [NoFieldFound] No field found: {TableName.FieldName}”.

Load Method

Our Load method will query Infusionsoft to retrieve a specific contact by Id. This method is most useful if you have your own database of contacts for which you have already set the Infusionsoft Contact Id or if you have a web page listing of contacts for whom you would like more detail.

Parameters
  • key (string) – The Infusionsoft API Key
  • contactId (int) – The contact id to find
  • selectedFields (string[]) – The contact fields to return in the results
Return Values

This method returns a single XmlRpcStruct.

Exceptions

XmlRpcFaultException Fault Code 5 If the Infusionsoft API cannot find a contact with the specified id, then this exception will be thrown with the message “Server returned a fault exception: [5] [RecordNotFound] Record was not found”.

XmlRpcFaultException Fault Code 10 If the Infusionsoft API cannot find a supplied contact field, then this exception will be thrown with the message “Server returned a fault exception: [10] [NoFieldFound] No field found: {TableName.FieldName}”.

Add Method

Our Add method will create a new contact record.

Parameters
  • key (string) – The Infusionsoft API Key
  • selectedFields (XmlRpcStruct) – The contact fields with corresponding values
Return Values

This method returns the newly created contact id (int).

Exceptions

XmlRpcFaultException Fault Code 10 If the Infusionsoft API cannot find a supplied contact field, then this exception will be thrown with the message “Server returned a fault exception: [10] [NoFieldFound] No field found: {TableName.FieldName}”.

Update Method

Our Update method will update an existing contact record.

Parameters
  • key (string) – The Infusionsoft API Key
  • contactId (int) – The contact id to update
  • selectedFields (XmlRpcStruct) – The contact fields with corresponding values
Return Values

This method returns the newly created contact id (int).

Exceptions

XmlRpcFaultException Fault Code 5 If the Infusionsoft API cannot find a contact with the specified id, then this exception will be thrown with the message “Server returned a fault exception: [5] [RecordNotFound] Record was not found”.

XmlRpcFaultException Fault Code 10 If the Infusionsoft API cannot find a supplied contact field, then this exception will be thrown with the message “Server returned a fault exception: [10] [NoFieldFound] No field found: {TableName.FieldName}”.

Step Two: Put It All Together

Get Contact By Id and Create a New Contact

[sourcecode language=”c”]

public void LoadAndUpdate()
{
IContactService contactService =
XmlRpcProxyGen.Create<IContactService>();

//Specify our API Url
contactService.Url =
"https://" + Api.AppName + ".infusionsoft.com/api/xmlrpc";

int contactId = 3;
string newEmailAddress3 = "barney.fife.3@u-gs.com";

try
{
//Contact Id to load

//Identify which fields should be returned in our queries
string[] queryFields = new[] { "Id", "EmailAddress3" };

XmlRpcStruct contact = contactService.Load(Api.Key, contactId, queryFields);

//We only want to add our new email address if it doesn’t exist or if has changed.
if (!contact.ContainsKey("EmailAddress3") || contact["EmailAddress3"] == newEmailAddress3)
{
//Specify the fields and values we wish to update on our contact record
XmlRpcStruct updatedContactData = new XmlRpcStruct
{
{ "EmailAddress3", newEmailAddress3 }
};

int updatedContactId = contactService.Update(Api.Key, contactId, updatedContactData);

Console.WriteLine("Successfully updated contact’s EmailAddress3 field to {0}.", newEmailAddress3);
}
else
{
Console.WriteLine("It looks like EmailAddress3 already has a value.");
}
}
catch (XmlRpcFaultException e)
{
if (e.FaultCode == 5)
{
Console.WriteLine("Yikes, contact with id {0} does not exist.", contactId);
}
else if (e.FaultCode == 10)
{
Console.WriteLine("Yikes, one of the contact fields was mistyped or doesn’t exist.");
}
else
{
throw;
}
}

}
[/sourcecode]

Lookup Contact By Email Address and Update or Create New

[sourcecode language=”c”]

public void FindAndUpsert()
{
IContactService contactService =
XmlRpcProxyGen.Create<IContactService>();

//Specify our API Url
contactService.Url =
"https://" + Api.AppName + ".infusionsoft.com/api/xmlrpc";

string emailAddressToLookup = "barney.fife.3@u-gs.com";
string emailAddress2 = "barney.fife.2@u-gs.com";

try
{
//Identify which fields should be returned in our queries
string[] queryFields = new[] { "Id", "EmailAddress2" };

XmlRpcStruct[] contacts = contactService.FindByEmail(Api.Key, emailAddressToLookup, queryFields);

//Specify the fields and values we wish to update on our contact record
XmlRpcStruct contactData = new XmlRpcStruct
{
{ "EmailAddress2", emailAddress2 }
};

//We are ignoring the fact that multiple contacts could have the specified email address
//and will only update the first one.
if (contacts.Length > 0)
{

//Get the contact id from our returned contact query fields
int theContactId = (int)contacts[0]["Id"];

int updatedContactId = contactService.Update(Api.Key, theContactId, contactData);

Console.WriteLine("Successfully updated contact:");
Console.WriteLine("EmailAddress2: {0}", emailAddress2);
Console.WriteLine("Matching contacts found: {0}", contacts.Length);
}
else
{
//We didn’t have any contacts with that email address,
//let’s create one

//Since we don’t have a contact with our lookup address,
//we will go ahead and add that field to the contactData
contactData.Add("Email", emailAddressToLookup);

int newContactId = contactService.Add(Api.Key, contactData);

Console.WriteLine("Successfully create contact.");
Console.WriteLine("Id: {0}.", newContactId);
Console.WriteLine("Email: {0}.", emailAddressToLookup);
Console.WriteLine("EmailAddress2: {0}.", emailAddress2);
}
}
catch (XmlRpcFaultException e)
{
if (e.FaultCode == 10)
{
Console.WriteLine("Yikes, one of the contact fields was mistyped or doesn’t exist.");
}
else
{
throw;
}
}
}

[/sourcecode]

By | 2014-08-26T12:15:02+00:00 June 21st, 2011|Categories: API, Infusionsoft|Tags: |0 Comments

About the Author:

Brad Ullery started off a little different – he was a jock in high school and college, and he found that he could talk to machines as easily as he could swing a bat. He spent years honing his craft, and has never met a technology he didn’t like (although there are some he prefers). A father or three amazing kids, a husband to an amazing wife, a baseball and hockey fan, Brad now spends his time architecting solutions designed to do one thing – deliver results.