Sharing a sample code that can be used ExecuteMultiple to bulk update the records.
private void MyForm_Load(object sender, EventArgs e) { var orgProxy = GetOrgProxy(); // place your fetch xml code to retrieve the records to be updated var fetchXML = @"FetchXMLQuery"; var multipleRequest = new ExecuteMultipleRequest() { // Assign settings that define execution behavior: continue on error, return responses. Settings = new ExecuteMultipleSettings() { ContinueOnError = true, ReturnResponses = true }, // Create an empty organization request collection. Requests = new OrganizationRequestCollection() }; // get all the records > 5000 var totalRecords = GetTotalRecordsFetchXML(orgProxy, fetchXML); // split the lst of entity to child list // specify the size of the batch i.e. 500 here var lstlstEntity = SplitList(totalRecords, 500); // loop through each of the list and create the request and add it to Execute Multiple Request foreach(var lstEntity in lstlstEntity) { multipleRequest.Requests.Clear(); foreach (var entity in lstEntity) { UpdateRequest createRequest = new UpdateRequest { Target = entity }; // add the field to be updated entity.Attributes["sab_referencenumber"] = "1000"; multipleRequest.Requests.Add(createRequest); } ExecuteMultipleResponse multipleResponse = (ExecuteMultipleResponse)orgProxy.Execute(multipleRequest); } } /// <summary> /// Function to split the list into child list /// </summary> /// <param name="locations"></param> /// <param name="nSize"></param> /// <returns></returns> public static List<List<Entity>> SplitList(List<Entity> locations, int nSize = 30) { var list = new List<List<Entity>>(); for (int i = 0; i < locations.Count; i += nSize) { list.Add(locations.GetRange(i, Math.Min(nSize, locations.Count - i))); } return list; } private static List<Entity> GetTotalRecordsFetchXML(OrganizationServiceProxy orgProxy, string fetchXML) { XDocument xDocument = XDocument.Parse(fetchXML); var fetchXmlEntity = xDocument.Root.Element("entity").ToString(); EntityCollection entityColl = new EntityCollection(); List<Entity> lstEntity = new List<Entity>(); int page = 1; do { entityColl = orgProxy.RetrieveMultiple(new FetchExpression( string.Format("<fetch version='1.0' page='{1}' paging-cookie='{0}'>" + fetchXmlEntity + "</fetch>", SecurityElement.Escape(entityColl.PagingCookie), page++))); lstEntity.AddRange(entityColl.Entities); } while (entityColl.MoreRecords); return lstEntity; } // Get service instance public static OrganizationServiceProxy GetOrgProxy() { IServiceManagement<IOrganizationService> orgServiceManagement = ServiceConfigurationFactory.CreateManagement<IOrganizationService>(new Uri("https://orgname.crm4.dynamics.com/XRMServices/2011/Organization.svc")); AuthenticationCredentials authCredentials = new AuthenticationCredentials(); authCredentials.ClientCredentials.UserName.UserName = "username"; authCredentials.ClientCredentials.UserName.Password = "password"; AuthenticationCredentials tokenCredentials = orgServiceManagement.Authenticate(authCredentials); return new OrganizationServiceProxy(orgServiceManagement, tokenCredentials.SecurityTokenResponse); }
Hope it helps..
I tried this in plugin around 2 months back for a scenario but it did not worked .. 😒but it was working fine from console application. I did not understand that. Please let me know if you have any insights on it. Thanks .
LikeLiked by 1 person
What error were you getting ?
Ideally there shouldn’t be any issue while using executemultiple within plugin
LikeLiked by 1 person
No any exception and it was not done it’s job that time. Might be issue with earlier version. I was facing this issue before v9.
LikeLiked by 1 person
Now its working,
https://softnovation.wordpress.com/2018/04/06/bulk-create-update-delete-for-dynamics-365/
LikeLike
Hi Nishant,
I have been trying ExecuteMultiple for a while. Its been working for updating, deleting records but its now working for Create operation. Any idea what should be the issue..? I am using Online Dynamics 365 V9 and getting SQL server Time Out Error each time.
Thanks for your time.
LikeLike
This could be an intermittent issue with CRM Online. Try after sometime it should work as update and delete are working for you. Also try increasing the timeout value for the service.
LikeLiked by 1 person
Hello, Whats the time taken to update 3-5 fields for 50,000 records using execute multiple ?
LikeLike