danesparza / MailChimp.NET

:envelope: .NET Wrapper for the MailChimp v2.0 API
MIT License
179 stars 119 forks source link

Trying to update Interest Groups, but getting strange exception... #107

Closed russellmcginnis closed 9 years ago

russellmcginnis commented 10 years ago

When trying to update an existing subscribers account based on selected InterestGroup names, I am getting the following exception thrown:

"\"Array\" is not a valid Interest Group in Grouping \"<My Group Name\" on the list: "} System.Exception {MailChimp.Errors.MailChimpAPIException}

The code is as follows:

MemberInfoResult mcMember = mc.GetMemberInfo(mcEmailListId, new List() { mcEmailParam }); if (mcMember.Data.Count == 1) { // The email is already registered with this list, but check the Interest Group and Group Names // If we dont have any interest group defined, we are not going to just sign an existing member // up for everything, so do nothing MergeVar mcEmailMergeVars = mcMember.Data[0].MemberMergeInfo; if (mcInterestGroup != null) { // All groupings should be here Grouping mcGrouping = mcEmailMergeVars.Groupings.Find(x => x.Id == mcInterestGroup.Id); if (mcGrouping != null) { // Reset the "Interested" flag to false, then go through adding back in the appropriate ones foreach (Grouping.GroupInterest tmp in mcGrouping.GroupInterests) { tmp.Interested = false; } // GroupNames is a comma seperated list of the InterestGroup Names foreach (string tmp in GroupNames.Split(',')) { Grouping.GroupInterest mcGroupInterest = mcGrouping.GroupInterests.Find(x => x.Name == tmp); if (mcGroupInterest != null) { mcGroupInterest.Interested = true; } } } } mc.UpdateMember(mcEmailListId, mcEmailParam, mcEmailMergeVars); }

Any ideas what i am doing wrong ?

danesparza commented 10 years ago

I know there were some changes to how MergeVars were handled recently. I have a feeling we need to review how MergeVars are getting handled and be consistent throughout the library

russellmcginnis commented 10 years ago

Using this as an example: https://github.com/Ecwid/ecwid-mailchimp/issues/15

I ended up further extending the MergeVars class to this:

[System.Runtime.Serialization.DataContract]
public class MyMergeVar : MergeVar {
    [System.Runtime.Serialization.DataMember(Name = "FNAME")]
    public string FirstName { get; set; }
    [System.Runtime.Serialization.DataMember(Name = "LNAME")]
    public string LastName { get; set; }
    [System.Runtime.Serialization.DataMember(Name = "groupings")]
    public List<MyInterestGrouping> MyGroupings { get; set; }
}

Where MyInterestGrouping class is defined as:

public class MyInterestGrouping {
    public int id { get; set; }
    public List<string> groups { get; set; }
    public MyInterestGrouping() {
        id = -1;
        groups = new List<string>();
    }
}

Now when I go to update the member's subscription properties - specifically their interest groups, I create new MyInterestGrouping objects for each Grouping pulled from the members MergeVars (after a call to GetMemberInfo) and update the specific Interest Group's group names.

Testing so far shows success.

solo812 commented 9 years ago

I have tried to Implement the "MyInterestGrouping" but cannot get it to work correctly.

I have downloaded the source and added the following to my MergeVar.cs file..

[System.Runtime.Serialization.DataContract]
public class MyMergeVar : MergeVar
{
    [System.Runtime.Serialization.DataMember(Name = "FNAME")]
    public string FirstName { get; set; }
    [System.Runtime.Serialization.DataMember(Name = "LNAME")]
    public string LastName { get; set; }

    [System.Runtime.Serialization.DataMember(Name = "groupings")]
    public List<MyInterestGrouping> MyGroupings { get; set; }
}

[DataContract]
public class MyInterestGrouping
{
     public int id { get; set; }
    public List<string> groups { get; set; }
    public MyInterestGrouping()
    {
        id = -1;
        groups = new List<string>();
    }
}

and then in my "MailChimpManager.cs" file, I thought I could change the "UpdateMember" method to the effect of...

   public EmailParameter UpdateMember(string listId, EmailParameter emailParam, MergeVar v, string emailType = "", bool replaceInterests = true)
    {
        const string apiAction = "lists/update-member";

        MyMergeVar m = new MyMergeVar();
        m.Language = v.Language;
        m.LocationData = v.LocationData;
        m.NewEmail = v.NewEmail;
        m.Notes = v.Notes;
        m.NewEmail = v.NewEmail;
        m.OptInIP = v.OptInIP;
        m.OptInTime = v.OptInTime;
        m.LocationData = v.LocationData;
        m.Notes = v.Notes;
        m.MyGroupings = new List<MyInterestGrouping>();

        foreach (Grouping g in v.Groupings)
        {     
            MyInterestGrouping ng = new MyInterestGrouping();
            ng.id = Convert.ToInt16(g.Id);
            foreach(var i in g.GroupInterests){
                if(i.Interested){
                    ng.groups.Add(i.Name);
                }
            }
            m.MyGroupings.Add(ng);
        }

        object args = new
        {
            apikey = this.APIKey,
            id = listId,
            email = emailParam,
            merge_vars = m,
            email_type = emailType,
            replace_interests = replaceInterests
        };

        //  Make the call:
        return MakeAPICall<EmailParameter>(apiAction, args);

    }

But it isn't working for some reason. Can you help me understand what I am missing please? I appreciate it.

mdissel commented 9 years ago

See this test when you use dictionaries https://github.com/danesparza/MailChimp.NET/blob/master/MailChimp.Tests/ListTests.cs#L245 else use https://github.com/danesparza/MailChimp.NET/blob/master/MailChimp.Tests/ListTests.cs#L183