This isn’t really related to a specific error; my code is not doing what it’s supposed to do.
I have a block of code – a foreach loop – that inititates a method in another class (“GetCustomersByGuid()”). This method is supposed to add a one-item customer collection (a List in CRM) to another customer collection. When looped, it should populate the customer collection with all ~2500 values.
The problem is, when the loop is finished, the customer collection is only populated with one customer – the final one in the list to be added to the collection. I have tried a couple of different things, but no luck.. because MS CRM shows only one customer in the collection (aka a marketing list, which is part of a campaign, etc etc).
Here is my code. Let me know your thoughts. Thank you!!
This is the main Program.cs code that is run:
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();
}
Here is the code for the GetCustomersByGUID method:
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)); //**
}
And here is the code for the GetCrmCustomers method:
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;
}
Thanks…
Your first line in
GetCustomersByGuid()isthis.Clear();, that’s removing all customers every time the method is called, that is, with each iteration of your loop. Then the last line in that methodthis.AddRange, but that will only add the current customer.I think
this.Clear();should go beforeforeach (string s in guids)in your methoddoCRMWork.