Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7580987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:05:12+00:00 2026-05-30T18:05:12+00:00

This isn’t really related to a specific error; my code is not doing what

  • 0

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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T18:05:13+00:00Added an answer on May 30, 2026 at 6:05 pm

    Your first line in GetCustomersByGuid() is this.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 method this.AddRange, but that will only add the current customer.

    I think this.Clear(); should go before foreach (string s in guids) in your method doCRMWork.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This isn't my code; I am trying to figure out what exactly this does.
this isn't asp.net specific, some of these apps are WinForms, also will be adding
This isn't really a programming question but more about programming and testing tools. Is
This isn't really clearly documented, but a shallow search reveals that RIM's RAPC compiler
This isn't really a technical question so I apolgoise if I ruffle any feathers!
this isn't really an issue, but more of a concern that I would appreciate
While this isn't strictly programming related, it's something I've run into as a web
This isn't solved, but I found out why: MySQL View containing UNION does not
This isn't really a problem, I am able to use the Android tools correctly,
This isn't much of a problem, really, it's entirely out of curiosity. For a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.