2012-03-03 181 views
0

这不是真正与特定错误有关;我的代码没有做它应该做的事情。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; 
    } 

谢谢...

+0

哦,人......它可能是这个。清除()。不知道为什么我直到现在才注意到它。我会测试它,并让你知道发生了什么......大声笑 – Paul 2012-03-03 00:57:22

回答

1

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

我认为this.Clear();应该在您的方法doCRMWorkforeach (string s in guids)之前。

+0

就是这样..我发布后不久自己发现了它。 D'oh ...谢谢! – Paul 2012-03-03 01:09:31