Skip to main content

Notifications

Dynamics 365 general forum

Import CSV File Via Console App Issues

Posted on by Microsoft Employee

Hey Guys,

Looking for some assistance if possible. First time developing with Dynamics and I have written a console app to process records from a csv file and enter them into Dynamics CRM. The code I have written seems to process all the records fine but when I look at the records in CRM once the console app has ran the total records do not equal to the records in the csv file.

The record set I am processing is around 15-20K and I am using the ExportMultipleRequest to process the batches in batches of 1000 due to the maxbatchsize being 1000 by default. Here is my code any help to diagnose the issue woudl be greatly appreciated.

  public static void InsertPlanSales()
        {
            // Get the CRM connection string
            CrmServiceClient crmConn = new CrmServiceClient(ConfigurationManager.ConnectionStrings["CRM"].ConnectionString);
            IOrganizationService crmService = crmConn.OrganizationServiceProxy;
            EntityCollection planSalesList = new EntityCollection();

            // Create an ExecuteMultipleRequest object.
            ExecuteMultipleRequest requestWithResults = new ExecuteMultipleRequest()
            {
                // Assign settings that define execution behavior: continue on error, return responses. 
                Settings = new ExecuteMultipleSettings()
                {
                    ContinueOnError = false,
                    ReturnResponses = true
                },
                // Create an empty organization request collection.
                Requests = new OrganizationRequestCollection()
            };

            try
            {
                string csvPath = ConfigurationManager.AppSettings["localFile"];

                //Read the contents of CSV file.
                var csvRows = File.ReadAllLines(csvPath);

                //Start at 2nd row as first is headers
                int startRow = 1;

                //Loop through the rows in the CSV.
                for (int i = startRow; i < csvRows.Length; i++)
                {
                    string currentRow = csvRows[i].Replace("\"", string.Empty);
                    string[] columnValues = currentRow.Split(',');
                    Entity planSaleDetails = new Entity("red_sales");

                    //Map values to Dynamics fields.
                    planSaleDetails.Attributes["red_anubisnumber"] = columnValues[0];
                    planSaleDetails.Attributes["red_allocationnumber"] = columnValues[1];
                    planSaleDetails.Attributes["new_agenttype"] = columnValues[2];
                    planSaleDetails.Attributes["red_fdtype"] = columnValues[3];
                    planSaleDetails.Attributes["new_sellername"] = columnValues[6];
                    planSaleDetails.Attributes["red_sellerhoname"] = columnValues[7];
                    planSaleDetails.Attributes["red_fdname"] = columnValues[8];
                    planSaleDetails.Attributes["red_fdhoname"] = columnValues[9];
                    planSaleDetails.Attributes["red_plannumber"] = columnValues[10];
                    planSaleDetails.Attributes["red_brander"] = columnValues[11];
                    planSaleDetails.Attributes["red_applicationtype"] = columnValues[12];
                    planSaleDetails.Attributes["red_paymenttype"] = columnValues[13];
                    planSaleDetails.Attributes["red_salestatus"] = columnValues[14];
                    planSaleDetails.Attributes["red_applicationdate"] = columnValues[15] == string.Empty ? null : (DateTime?)Convert.ToDateTime(columnValues[15]).ToUniversalTime();
                    planSaleDetails.Attributes["red_startdate"] = columnValues[16] == string.Empty ? null : (DateTime?)Convert.ToDateTime(columnValues[16]).ToUniversalTime();
                    planSaleDetails.Attributes["red_cancellationdate"] = columnValues[17] == string.Empty ? null : (DateTime?)Convert.ToDateTime(columnValues[17]).ToUniversalTime();
                    planSaleDetails.Attributes["red_scorecardchannel"] = columnValues[18];
                    planSaleDetails.Attributes["new_salesvalueatpos"] = columnValues[19] == string.Empty ? null : (Double?)Convert.ToDouble(columnValues[19]);
                    planSaleDetails.Attributes["red_salesvalueatalloc"] = columnValues[20] == string.Empty ? null : (Double?)Convert.ToDouble(columnValues[20]);
                    planSaleDetails.Attributes["red_salestype"] = columnValues[21];
                    planSaleDetails.Attributes["red_maturitystatus"] = columnValues[22];
                    planSaleDetails.Attributes["red_plancategory"] = columnValues[23];

                    planSalesList.Entities.Add(planSaleDetails);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            int remainingRecords = planSalesList.Entities.Count();

            foreach (var entity in planSalesList.Entities)
            { 
                CreateRequest createRequest = new CreateRequest { Target = entity };
                requestWithResults.Requests.Add(createRequest);
                remainingRecords--;
                if (requestWithResults.Requests.Count == 1000 || remainingRecords == 0)
                {

                    var responseWithResults = crmService.Execute(requestWithResults) as ExecuteMultipleResponse;
                    requestWithResults.Requests.Clear();
                    Console.WriteLine("{0}.{1}.{2}", "1000 Records Processed", "Remaining Records ", remainingRecords );
                }

            }
 
        }


Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans