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.

Cache Customer ContactList _List in SQL then back to _List

SOLVED
Go to solution
Highlighted
Advisor

Cache Customer ContactList _List in SQL then back to _List

I'm using a cache of the ContactList collection commited to SQL with a datetime LastUpdate field. In my xml INI file I have a mode & interval (Days, Hours, Minutes) and if less than, then I use table[1] of my CRM data to restore _List as a ContactList collection available to all routines in my SSIS script.

 

         if (bRefresh == false)
            {
                ContactList tmpList = new ContactList();
                List<ContactList> newList = new List<ContactList>();

                foreach (DataRow rt in dt.Rows)
                {
                    tmpList.Id = (string)rt["Id"];
                    tmpList.Link = (string)rt["Link"];
                    tmpList.Name = (string)rt["Name"];
                    tmpList.OptInDefault = Convert.ToBoolean(rt["OptInDefault"]);
                    tmpList.ShortName = (string)rt["ShortName"];
                    tmpList.SortOrder = Convert.ToInt32(rt["SortOrder"]);
                    tmpList.SystemList = ContactSystemList.Undefined;
                    newList.Add(tmpList);
                }
                if (_List == null) { _List = newList; }
            }

I am NOT a List<> master, so this may be a simple fix...

 

Iterating through each rt newList receives the first of three, ConstantContactBO.ContactList {Id=1}, then the second, however on the second and subsequent third, each list item is the last; so after the second Add both _list 0 & list 1 have the same {Id=2} and on the third all three have {Id=3} have all look the same as the final third. I end up with three items in the collection, but they're all the same!

 

My later eval of current ContactLists in _Lists and new lists, (if they're the same no update occurs), an update does occur because the lists with {Id=1} & {Id=2} don't match the on list {Id=3}, so only the one {Id=3} is sent and the contact is unsubscribed from the two other lists.

  

// eval matching listID's between CtCt's User's contactLists & CRMLists rList
if (!ctList.IsSystemList & (ctList.Id.ToString() == sINIListID))
{
    bInCtCtList = false;
    foreach (ContactList contList in tmpContToUpdateLists)
    {
        if (contList.Id == sINIListID) { bInCtCtList = true; }
    }
    if (action == Action.New) { bInCtCtList = true; }

    // if Yes, add list to tmpListToCt                                
    if (sColValue.ToLower() == sYes | (bInCtCtList == true & bKeepCtCt == true))
    {
        bAdded = true;
        tmpListToCt.Add(ctList);
    }
    // if was Added or keep = T & in the lst then true, else false                                
    if (bAdded == true & (bKeepCtCt == true & bInCtCtList == false)
            | bAdded == true & bInCtCtList == false
            | bAdded == false & bInCtCtList == true & bKeepCtCt == false)
    {
        bUpdateListsCtCt = true;
    }
    if (bAdded == true) { break; }

Here in the above snipit, ctList is _List and we see in tmpListToCt.Add(ctList) add the list successfully (tested many times works great), but not so much for the first snipit.

 

I hope there's something easy as a fix to why the first snipit newList.Add() has the odd behavior of adding another list to the collection but making all prior list items equal to the one. TIA

 

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Advisor

Re: Cache Customer ContactList _List in SQL then back to _List

found it... reset tmpList = new ContactList();

 

if (bRefresh == false)
{
    ContactList tmpList = new ContactList();                
    List<ContactList> newList = new List<ContactList>();
    foreach (DataRow rt in dt.Rows)
    {
        tmpList.Id = (string)rt["Id"];
        tmpList.Link = (string)rt["Link"];
        tmpList.Name = (string)rt["Name"];
        tmpList.OptInDefault = Convert.ToBoolean(rt["OptInDefault"]);
        tmpList.ShortName = (string)rt["ShortName"];
        tmpList.SortOrder = Convert.ToInt32(rt["SortOrder"]);
        tmpList.SystemList = ContactSystemList.Undefined;                    
        newList.Add(tmpList);
        tmpList = new ContactList();
    }
    if (_List == null) { _List = newList; }
}
return;
}

 ...there may be more things to clean up here, but the contact's selected lists in the CRM are now reflected in CtCt

 

View solution in original post

3 REPLIES 3
Highlighted
Advisor

Re: Cache Customer ContactList _List in SQL then back to _List

Doc2_p.jpg

Highlighted
Advisor

Re: Cache Customer ContactList _List in SQL then back to _List

found it... reset tmpList = new ContactList();

 

if (bRefresh == false)
{
    ContactList tmpList = new ContactList();                
    List<ContactList> newList = new List<ContactList>();
    foreach (DataRow rt in dt.Rows)
    {
        tmpList.Id = (string)rt["Id"];
        tmpList.Link = (string)rt["Link"];
        tmpList.Name = (string)rt["Name"];
        tmpList.OptInDefault = Convert.ToBoolean(rt["OptInDefault"]);
        tmpList.ShortName = (string)rt["ShortName"];
        tmpList.SortOrder = Convert.ToInt32(rt["SortOrder"]);
        tmpList.SystemList = ContactSystemList.Undefined;                    
        newList.Add(tmpList);
        tmpList = new ContactList();
    }
    if (_List == null) { _List = newList; }
}
return;
}

 ...there may be more things to clean up here, but the contact's selected lists in the CRM are now reflected in CtCt

 

View solution in original post

Highlighted
Moderator

Re: Cache Customer ContactList _List in SQL then back to _List

I was actually just in the process of writing that same potential solution.  The behavior you're describing is consistent with how collections work in some languages.  The .Add() method is adding a reference to the object you created and had pointed to in tmpList.  Since you weren't creating new objects for each entity, you were actually changing the contents of only a single object that just had many references to it. 

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