2012-03-03 151 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