How to update more than 5000 records using c# in mscrm

//////ConnectionString////
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
  </startup>
  <connectionStrings>
    <add name="D365.8.2" connectionString="AuthType=Office365;Username=crmdev@amllp.com; Password=veDmrC@123;Url=https://armanino.crm.dynamics.com"/>
    <!--<add name="D365.8.2" connectionString="AuthType=IFD; Username=vikas@go-gravity.com; Password=gravity2@@;Url=https://gsdev.go-gravity.com/GSDEV"/>-->
  </connectionStrings>
</configuration>
/////////How to update more than 5000 records using c# in mscrm//////////
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Tooling.Connector;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
////////References//////
//Microsoft.Crm.Sdk.Proxy
//Microsoft.CSharp
//Microsoft.IdentityModel
//Microsoft.IdentityModel.Clients.ActiveDirectory
//Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms
//Microsoft.Xrm.Sdk
//Microsoft.Xrm.Sdk.Deployment
//Microsoft.Xrm.Sdk.Workflow
//Microsoft.Xrm.Tooling.Connector
//System
//System.Configuration
//System.Core
//System.Data
//System.Data.DataSetExtensions
//System.Net.Http
//System.Runtime.Serialization
//System.Security
//System.ServiceModel
//System.Xml
//System.Xml.Linq
namespace BANTScoreUpdate
{
    class BANTScoreUpdateToOpportunities
    {
        static void Main(string[] args)
        {
            CrmServiceClient crmSvc = new CrmServiceClient(ConfigurationManager.ConnectionStrings["D365.8.2"].ConnectionString);
            IOrganizationService orgService = (IOrganizationService)crmSvc;
            BANTScoreUpdateToOpportunities obj = new BANTScoreUpdateToOpportunities();
            obj.UpdateBANCTScoreOnAllOpportunites(orgService);
            Console.WriteLine("Exe completed successfull");
            Console.ReadLine();
        }
        private void UpdateBANCTScoreOnAllOpportunites(IOrganizationService orgService)
        {
            int queryCount = 5000;
           
            int pageNumber = 1; 

            int recordCount = 0;
           
           
            QueryExpression pagequery = new QueryExpression();
            pagequery.EntityName = "opportunity"; 
            pagequery.ColumnSet.AddColumns("am_budgetyesno", "am_customerbudgetamt", "am_economicbuyer", "am_need", "new_estgolive", "am_targetdecisiondate");

            pagequery.PageInfo = new PagingInfo();
            pagequery.PageInfo.Count = queryCount;
            pagequery.PageInfo.PageNumber = pageNumber;
            pagequery.PageInfo.PagingCookie = null;

            Console.WriteLine("Retrieving sample account records in pages...\n");
            Console.WriteLine("#\tAccount Name\t\tEmail Address");

            while (true)
            {
                // Retrieve the page.
                EntityCollection results = orgService.RetrieveMultiple(pagequery);
                if (results.Entities != null)
                {
                    // Retrieve all records from the result set.
                    foreach (Entity existingOpportunity in results.Entities)
                    {
                        int BANTScore = 0;
                        if (existingOpportunity.Contains("am_budgetyesno"))
                            BANTScore++;
                        if (existingOpportunity.Contains("am_customerbudgetamt"))
                            BANTScore++;
                        if (existingOpportunity.Contains("am_economicbuyer"))
                            BANTScore++;
                        if (existingOpportunity.Contains("am_need"))
                            BANTScore++;
                        if (existingOpportunity.Contains("new_estgolive"))
                            BANTScore++;
                        if (existingOpportunity.Contains("am_targetdecisiondate"))
                            BANTScore++;

                        Entity opportunityToUpdate = new Entity(existingOpportunity.LogicalName, existingOpportunity.Id);
                        opportunityToUpdate["am_bantscore"] = BANTScore;
                        orgService.Update(opportunityToUpdate);
                    }
                }

                // Check for more records, if it returns true.
                if (results.MoreRecords)
                {
                    Console.WriteLine("\n****************\nPage number {0}\n****************", pagequery.PageInfo.PageNumber);
                    Console.WriteLine("#\tAccount Name\t\tEmail Address");

                    pagequery.PageInfo.PageNumber++;
                    pagequery.PageInfo.PagingCookie = results.PagingCookie;
                }
                else
                {
                    break;
                }
            }

        }
    }
}

Comments

Popular posts from this blog

Basic Plugin Code in D365 using C#

CURD (Create, Update, Retrieve and Delete) Operation in D365 using Power Shell Script

Meta Data Using WebApiRequest