The ExternalActionService that is used for Cloud Connectors to allow access to the contacts in a marketing automation program does a great job of retrieving and managing the status on contacts, companies, and prospects in a program step.  However, as it does that, it is only referencing those members by an id such as a contact id.  To do anything of interest with those Contacts, you will likely want to flesh them out with the rest of the data that is known on them as contacts.

 

To do this, the code snippet we’ll look at queries the standard Eloqua API, using the generic setup code we looked at earlier, passes in a set of fields to populate, and returns with a set of values for those fields.

 

 

        public List<EloquaContact> GetContactsFullDetail(List<EloquaContact> tmpContacts)

        {

            int intTotalContacts;

            int intCurrentContactCount;

            int intCurrentBatchSize;

            int intRetrieveBatchSize = 50;

            int intCurrentContactIndex = 0;

            int intCurrentArrayIndex = 0;

 

            int internalErrorCount = 0;

 

            bool blnResetNullContacts = false;

 

            List<EloquaContact> tmpFullContactList = new List<EloquaContact>();

 

            EloquaContact tmpContact;

 

            try

            {

                // Build a Contact Entity Type object

                EloquaServiceNew.EntityType entityType = new EloquaServiceNew.EntityType();

 

                entityType.ID = 0;

                entityType.Name = "Contact";

                entityType.Type = "Base";

 

 

                // Create a new list containing the fields you want populated

 

                List<string> fieldList = new List<string>();

                try

                {

                    fieldList = PopulateFieldList(AutomatedStep.EloquaBaseType.Contact);

                }

                catch (Exception ex)

                {

                    Trace.TraceWarning(String.Format("Exception in EloquaInstance:GetContactsFullDetail - populate field list {0} :: {1} :: {2} ", ex.Message, ex.InnerException, strInstanceName), "Exception");

                    throw;

                }

                // Build a Dynamic Entity array to store the results

 

                EloquaServiceNew.DynamicEntity[] retrievedEntities;

 

                // Only fields defined in the field list are returned

                strErrorDebug += "Beginning batch ";

 

                if (fieldList.Count > 0)

                {

 

                    // Execute the request and return only the selected Entity fields

                    // Set the ID of the Contact Entity

                    intTotalContacts = tmpContacts.Count();

                    intCurrentContactCount = 0;

 

 

                    int[] ids;

                    intCurrentContactCount = 0;

                    intCurrentBatchSize = Math.Min((intTotalContacts - intCurrentContactCount), intRetrieveBatchSize);

                    strErrorDebug += "While loop ";

                    while (intCurrentBatchSize > 0)

                    {

 

                        ids = new int[intCurrentBatchSize];

                        intCurrentArrayIndex = 0;

                        try{

                            for (intCurrentContactIndex = intCurrentContactCount; intCurrentContactIndex < (intCurrentContactCount + intCurrentBatchSize); intCurrentContactIndex++)

                            {

                                ids[intCurrentArrayIndex] = tmpContacts.ElementAt(intCurrentContactIndex).ContactID;

                                intCurrentArrayIndex++;

                            }

                        }

                        catch (Exception ex)

                        {

                            Trace.TraceWarning(String.Format("Exception in EloquaInstance:GetContactsFullDetail populate array {0} :: {1} :: {2}  ", ex.Message, ex.InnerException, strInstanceName), "Exception");

                            throw;

                        }

                        try

                        {

                            retrievedEntities = serviceProxy.Retrieve(entityType, ids, fieldList.ToArray());

                        }

                        catch (Exception ex)

                        {

                            Trace.TraceWarning(String.Format("Exception in EloquaInstance:GetContactsFullDetail retrieve {0} :: {1} :: {2}  ", ex.Message, ex.InnerException, strInstanceName), "Exception");

                            throw;

                        }

 

                        if (retrievedEntities.Length > 0)

                        {

                            int intIDCount = 0;

                            foreach (EloquaServiceNew.DynamicEntity dynamicEntity in retrievedEntities)

                            {

                                if (dynamicEntity != null)

                                {

                                    try

                                    {

                                        tmpContact = new EloquaContact();

                                        tmpContact.ContactID = dynamicEntity.Id;

                                        PopulateContactFromResult(tmpContact, dynamicEntity.FieldValueCollection);

                                        tmpContact.ExternalActionID = tmpContacts.Find(delegate(EloquaContact ec) { return ec.ContactID == tmpContact.ContactID; }).ExternalActionID;

                                        tmpFullContactList.Add(tmpContact);

                                    }

                                    catch (Exception ex)

                                    {

                                        Trace.TraceWarning(String.Format("Exception in EloquaInstance:GetContactsFullDetail populate contact {0} :: {1} :: {2}  ", ex.Message, ex.InnerException, strInstanceName), "Exception");

                                        internalErrorCount++;

                                        if (internalErrorCount > 4)

                                            throw;

                                    }

                                }

                                else

                                {

                                    blnResetNullContacts = true;

                                }

                                intIDCount++;

                            }

                        }

 

                        intCurrentContactCount += intCurrentBatchSize;

                        intCurrentBatchSize = Math.Min((intTotalContacts - intCurrentContactCount), intRetrieveBatchSize);

                    }//end batch retrieve while loop

 

 

                }

 

            }

            catch (Exception ex)

            {

                Trace.TraceWarning(String.Format("Exception in EloquaInstance:GetContactsFullDetail {0} :: {1} :: {2} ", ex.Message, ex.InnerException, strInstanceName), "Exception");

                throw;

            }

 

            if (blnResetNullContacts)

            {

                //handle null contacts in the step

            }

 

            return tmpFullContactList;

        }

 

 

 

This code snippet calls PopulateFieldList to create a list of fields (which will ultimately be passed to the API as an array of strings). You’ll want to implement your own logic here to define the set of fields (by their internal name – you’ll see this when you list the contact fields via the API, but it is not displayed to the user) that you want to retrieve.  The values for the fields will generally look like “C_EmailAddress”, or “C_Custom_Field1”.  Based on the field mappings that have been provided by the users of your application, you will have this list of fields available and can return it here in the PopulateFieldList function you implement.

 

The other function that is seen here that you’ll want to implement your own version of is:

PopulateContactFromResult(tmpContact, dynamicEntity.FieldValueCollection);

 

This takes a contact object that we have defined for our own use, and a dynamicEntity.FieldValueCollection, and parses through those to populate the Contact.  In this example, we have fields for FirstName, LastName, and Company on the EloquaContact object definition:

 

    public class EloquaContact

    {

        public int ContactID { get; set; }

 

        public string EmailAddress { get; set; }

        public int ExternalActionID { get; set; }

       public string FirstName { get; set; }

       public string LastName { get; set; }

       public string Company { get; set; }

 

 

        public EloquaContact()

        {

            ContactID = 0;

            EmailAddress = "";

            ExternalActionID = 0;

        }

   }

 

 

We pass these into the PopulateContactFromResult function to parse the FieldValueCollection to look for the right fields to grab values from.  In this example, we use a custom function called GetFieldNameValues to return a set of values and fields to populate.  This would return a dictionary that looked like the following:

 

“FirstName”, “C_FirstName”

“LastName”, “C_LastName”

“Company”, “C_CompanyName1”

 

 

    private void PopulateContactFromResult(EloquaContact tmpContact, IEnumerable<KeyValuePair<string, string>> FieldValueCollection)

        {

            string strCurrentField = "";

            string strEmailAddressField = "";

 

            try

            {

                Dictionary<string, string> dctFieldNameValues = GetFieldNameValues();

 

                foreach (KeyValuePair<string, string> keyValPair in FieldValueCollection)

                {

                    if (dctFieldNameValues.ContainsValue(keyValPair.Key))

                    {

                        strCurrentField = dctFieldNameValues.First(delegate(KeyValuePair<string, string> x) { return x.Value == keyValPair.Key; }).Key;

                        tmpContact.SetFieldValue(strCurrentField, keyValPair.Value);

                    }

                    if (keyValPair.Key == strEmailAddressField)

                        tmpContact.EmailAddress = keyValPair.Value;

                }

            }

            catch (Exception ex)

            {

                Trace.TraceWarning(String.Format("Exception in EloquaInstance:PopulateContactFromResult {0} :: {1} :: {2}", ex.Message, ex.InnerException, strCurrentField), "Exception");

                throw;

            }

 

        }

 

 

With this value retrieved, we will then call a function on the Contact object to set this value:

 

tmpContact.SetFieldValue(“FirstName”, “Bob”);

 

This approach can be implemented in many ways, depending on your application’s approach to managing data on contacts.  The way in which data is retrieved from the Eloqua API, however, will remain common.