CRM编码逻辑错误

时间:2012-03-03 00:48:10

标签: c# logic dynamics-crm crm

这与特定错误无关;我的代码没有做它应该做的事情。

我有一个代码块 - 一个foreach循环 - 在另一个类中启动一个方法(“GetCustomersByGuid()”)。此方法应该将一项客户集合(CRM中的List)添加到另一个客户集合。循环时,它应该使用所有~2500个值填充客户集合。

问题是,当循环结束时,客户集合仅填充一个客户 - 列表中的最后一个客户将添加到集合中。我尝试了几个不同的东西,但没有运气..因为MS CRM在集合中只显示了一个客户(也就是营销列表,这是广告系列的一部分等)。

这是我的代码。让我知道你的想法。谢谢!!

这是运行的主要Program.cs代码:

private static void doCRMWork()
    {
        try
        {
            CRMConnectionHelper.Authenticate();

            Console.WriteLine("Begin.");

            //queryCrm();

            const string f = @"C:\WindowsApps\CRM\crm_interface3\data\CRMGuids_HM2011_v2.txt";

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

            using (StreamReader r = new StreamReader(f))
            {
                string line;
                while ((line = r.ReadLine()) != null)
                {
                    guids.Add(line);
                }
            }

            CustomerCollection customers = new CustomerCollection();

            foreach (string s in guids)
            {
                customers.GetCustomersByGuid(s);
                Console.WriteLine("Customer added");

                //Overwriting the customer collection every loop iteration...
            }

            Campaign cmpn = new Campaign();
            cmpn.CreateCampaign("2011 Test3 Holiday Mailing");

            Console.WriteLine("Campaign and activity created. Press enter to continue.");
            Console.ReadLine();

            cmpn.AddCustomersToCampaign(customers);
            Console.WriteLine("Created marketing list and added customers to campaign.");
            Console.WriteLine("End.");
            Console.ReadLine();
        }

以下是GetCustomersByGUID方法的代码:

    public void GetCustomersByGuid(String GUID)
    {
        this.Clear();
        ConditionExpression condition_contact = new ConditionExpression();
        condition_contact.Operator = ConditionOperator.Equal;
        condition_contact.Values = new String[] { GUID }; //Creates one-item array for current GUID
        condition_contact.AttributeName = "contactid";
        FilterExpression filter_contact = new FilterExpression();
        filter_contact.FilterOperator = LogicalOperator.And;
        filter_contact.Conditions = new ConditionExpression[] { condition_contact };
        this.AddRange(GetCrmCustomers(filter_contact, Customer.CustomerTypes.Contact));  //**

    }

以下是GetCrmCustomers方法的代码:

    private static CustomerCollection GetCrmCustomers(FilterExpression filter, Customer.CustomerTypes customerType)
    {
        CustomerCollection customers = new CustomerCollection();
        if (customerType == Customer.CustomerTypes.Contact)
        {
            QueryExpression query = new QueryExpression();
            query.ColumnSet = new AllColumns();
            query.EntityName = EntityName.contact.ToString();
            if (filter != null)
            {
                query.Criteria = filter;
            }
            BusinessEntityCollection bc = new BusinessEntityCollection();
            try 
            { 
                bc = CRMInterface.crmService.RetrieveMultiple(query); 
            }
            catch 
            { 
            }
            if (bc.BusinessEntities != null)
            {
                for (int i = 0; i < bc.BusinessEntities.Length; i++)
                {
                    //I think BusinessEntities.Length is 1 because of 'new string[] {GUID}' -- one element.
                    Console.WriteLine("i is: " + i + "and bc.BusinessEntities.Length is:  " + bc.BusinessEntities.Length);
                    Customer customer = new Customer();
                    customer.CustomerType = Customer.CustomerTypes.Contact;
                    customer.GUID = ((contact)bc.BusinessEntities[i]).contactid.Value.ToString();
                    customer.Firstname = ((contact)bc.BusinessEntities[i]).firstname == null ? "" : ((contact)bc.BusinessEntities[i]).firstname;
                    customer.Middlename = ((contact)bc.BusinessEntities[i]).middlename == null ? "" : ((contact)bc.BusinessEntities[i]).middlename;
                    customer.Lastname = ((contact)bc.BusinessEntities[i]).lastname == null ? "" : ((contact)bc.BusinessEntities[i]).lastname;
                    customer.Suffix = ((contact)bc.BusinessEntities[i]).suffix == null ? "" : ((contact)bc.BusinessEntities[i]).suffix;
                    customer.Email = ((contact)bc.BusinessEntities[i]).emailaddress1 == null ? "" : ((contact)bc.BusinessEntities[i]).emailaddress1.Trim().ToLower();
                    customer.Donotbulkemail = ((contact)bc.BusinessEntities[i]).donotbulkemail == null ? false : ((contact)bc.BusinessEntities[i]).donotbulkemail.Value;
                    customers.Add(customer); //***
                    //So customers is the collection that is added to the end of the list each time.

                    //This loop is only run once in this class, but is run over and 
                    //over again in the program class.
                    //It might be 

                }
            }
        }
        else
        {
            QueryExpression query = new QueryExpression();
            query.ColumnSet = new AllColumns();
            query.EntityName = EntityName.account.ToString();
            query.Criteria = filter;
            BusinessEntityCollection bc = new BusinessEntityCollection();
            try 
            { 
                bc = CRMInterface.crmService.RetrieveMultiple(query); 
            }
            catch 
            { 
            }

            if (bc.BusinessEntities != null)
            {
                for (int i = 0; i < bc.BusinessEntities.Length; i++)
                {
                    Customer customer = new Customer();
                    customer.CustomerType = Customer.CustomerTypes.Account;
                    customer.GUID = ((account)bc.BusinessEntities[i]).accountid.Value.ToString();
                    customer.Name = ((account)bc.BusinessEntities[i]).name == null ? "" : ((account)bc.BusinessEntities[i]).name;
                    customer.Email = ((account)bc.BusinessEntities[i]).emailaddress1 == null ? "" : ((account)bc.BusinessEntities[i]).emailaddress1.Trim().ToLower();
                    customer.Donotbulkemail = ((account)bc.BusinessEntities[i]).donotbulkemail == null ? false : ((account)bc.BusinessEntities[i]).donotbulkemail.Value;
                    customers.Add(customer);
                }
            }
        }
        return customers;
    }

...谢谢

1 个答案:

答案 0 :(得分:1)

GetCustomersByGuid()中的第一行是this.Clear();,每次调用该方法时都会删除所有客户,即每次循环迭代时都会删除。然后是该方法this.AddRange中的最后一行,但这只会添加当前客户。

我认为this.Clear();应该在方法foreach (string s in guids)doCRMWork之前。{/ p>