這不是真正與特定錯誤有關;我的代碼沒有做它應該做的事情。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;
}
謝謝...
哦,人......它可能是這個。清除()。不知道爲什麼我直到現在才注意到它。我會測試它,並讓你知道發生了什麼......大聲笑 – Paul 2012-03-03 00:57:22