cancel
Showing results for 
Search instead for 
Did you mean: 

Cache Customer ContactList _List in SQL then back to _List

SOLVED
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

 

3 REPLIES
Advisor

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

Doc2_p.jpg

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

 

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
Still need help?
You can post a new message in the Community or find us on Twitter Mon-Fri 8am - 8pm ET. We've got real people waiting to help you out. Click below to start a conversation!