Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

On Bulk Create Run Asynchronous plugins

Posted on by 745

Hi,

I have a requirement to update data around 1 lakh++ records

create log of the data in new Entity with old and new values same as audit history

so my main question is When we create data through 100 requests of  BULK create 

will the Async Plugin registered on the entity will run as here it might be triggered thousands of times at the same time

Note :Console App not required as i want it everything in Solution

  • Suggested answer
    MMK Profile Picture
    MMK 745 on at
    RE: On Bulk Create Run Asynchronous plugins

    There is no Work around for Updating Lakhs of Data with CRM through Plugin or Workflow Only Console App could do that or Any API you can deploy both is  same thing

    The Async Plugins will run and keep waiting untill they are executed or killed

  • ajyendra Profile Picture
    ajyendra 1,730 on at
    RE: On Bulk Create Run Asynchronous plugins

    okay. I get it you are using online version. That why

    Did you check your code take more than 2 minutes?

    hit and trials for getting How much data create? so that you can create batches ?

    Otherwise In Dynamics 365 (CRM) Online, there is no going around it.

    Check this forum(they uses another service like azure its billable) :

    community.dynamics.com/.../plugin-timeout-exception-in-custom-workflow-activity

    NOTE:- if lot of System Jobs Status is in "Waiting for resources". It will create some performance issue. Some times it will not run some processes until all the Waiting for resources Jobs is not done.

    Hope Someone have different thought about that. Best of Luck

  • MMK Profile Picture
    MMK 745 on at
    RE: On Bulk Create Run Asynchronous plugins

    @Ajyendra : On the latest CRM we can't register Plugin or workflow in Isolation mode to None

  • ajyendra Profile Picture
    ajyendra 1,730 on at
    RE: On Bulk Create Run Asynchronous plugins

    @Moin Khan ,

    Check this Forum:

    community.dynamics.com/.../workflows-to-create-new-records

    Hope it helps

  • MMK Profile Picture
    MMK 745 on at
    RE: On Bulk Create Run Asynchronous plugins

    @ajyendra can you please share steps or link

    to register and run custom workflow more than 2 minutes and in background

    how much more execution time we can extend to

    If this possible then I can simply divide the records and run workflow's instances to update

  • ajyendra Profile Picture
    ajyendra 1,730 on at
    RE: On Bulk Create Run Asynchronous plugins

    If you want your custom workflow run more than 2 minutes register your workflow in Mode=none & also make sure to run workflow in background .

  • MMK Profile Picture
    MMK 745 on at
    RE: On Bulk Create Run Asynchronous plugins

    @ajyendra Custom workflow also runs 2 minutes only

    another thing  It will definitely be going to take more than an hour 

  • Suggested answer
    ajyendra Profile Picture
    ajyendra 1,730 on at
    RE: On Bulk Create Run Asynchronous plugins

    Okay. I misunderstood.

    Actually plugin timeout is 2 minutes(Either Sync or Async).

    if your code take more than 2 minute time it will give you some error like this:

    Unhandled Exception: Microsoft.Crm.CrmException: Unexpected exception from plug-in (Execute): Xrm.Plugin.GenerateInspectionDetails.GenerateInspection: System.TimeoutException: Couldn’t complete execution of the Xrm.Plugin.GenerateInspectionDetails.GenerateInspection plug-in within the 2-minute limit.

    You can try if it gave you this error this means your code take more than 2 minutes time to execute. if not it will work.

    if you find out the error above 2-minute limit. So better go through Custom Workflow. There is no time limit for workflow.

  • MMK Profile Picture
    MMK 745 on at
    RE: On Bulk Create Run Asynchronous plugins

    It will be regularly used by end user so @ajyendra  Console App is not solution

    From JavaScript batch operation I will create the records

    My concern is will the async plugins registered on the create of the entity will they run or fail due to large number of calls at same time

  • ajyendra Profile Picture
    ajyendra 1,730 on at
    RE: On Bulk Create Run Asynchronous plugins

    Hi,

    As @Joana Mentioned . i also done bulk update through console application around 3 lac records.

    provide you a code in attachment

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Client;
    using System.Net;
    using System.ServiceModel.Description;
    using Microsoft.Xrm.Sdk.Messages;
    using Microsoft.Xrm.Sdk.Query;
    using System.Xml;
    using System.IO;
    
    namespace ProcessCancel
    {
        class Program
        {
            static void Main(string[] args)
            {
                ClientCredentials credentials = new ClientCredentials();
    
                credentials.UserName.UserName = "xxxx@yyyy.onmicrosoft.com"; //Username
    
                credentials.UserName.Password = "abcabcac"; //Password
    
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    
                Uri serviceUri = new Uri("https://xxxxxxxx.api.crm.dynamics.com/XRMServices/2011/Organization.svc"); //Organization url
    
                OrganizationServiceProxy proxy = new OrganizationServiceProxy(serviceUri, null, credentials, null);
    
                proxy.EnableProxyTypes();
    
                IOrganizationService service = (IOrganizationService)proxy;
                Console.WriteLine("Connected");
                UpdateInBulk(proxy);
    
            }
    
            public static void UpdateInBulk(OrganizationServiceProxy _orgService)
            {
                Int32 pageNumber = 1;
                // Initialize the number of records.
                int fetchCount = 1000;
                // Specify the current paging cookie. For retrieving the first page,
                // pagingCookie should be null.
                string pagingCookie = null;
                string fetchXml = @"
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                                
                                
                                
                                     
                                 
                            
                            ";
                while (true)
                {
                    // Build fetchXml string with the placeholders.
                    string xml = CreateXml(fetchXml, pagingCookie, pageNumber, fetchCount);
                    // Excute the fetch query and get the xml result.
                    RetrieveMultipleRequest fetchRequest1 = new RetrieveMultipleRequest
                    {
                        Query = new FetchExpression(xml)
                    };
                    EntityCollection returnCollection = ((RetrieveMultipleResponse)_orgService.Execute(fetchRequest1)).EntityCollection;
                    List updateCollection = new List();
                    foreach (var c in returnCollection.Entities)
                    {
    
                        var updateEntity = new Entity("asyncoperation");
                        updateEntity.Id = c.Id;
                        updateEntity["statuscode"] = new OptionSetValue(32);  // 32 is for status cancelled                      
                       updateEntity["statecode"] = new OptionSetValue(3);
                        try
                        {
                            updateCollection.Add(updateEntity);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("Error1");
                            Console.ReadKey();
                        }
                    }
                    BulkUpdateSystemJobRecords(_orgService, updateCollection);
                    // Check for morerecords, if it returns 1.
                    if (returnCollection.MoreRecords)
                    {
                        Console.WriteLine("\n****************\nPage number {0}\n****************", pageNumber);
                        // Increment the page number to retrieve the next page.
                        pageNumber  ;
                        // Set the paging cookie to the paging cookie returned from current results.
                        pagingCookie = returnCollection.PagingCookie;
                        
                        
                    }
                    else
                    {
                        // If no more records in the result nodes, exit the loop.
                        break;
                    }
                }
            }
            public static void BulkUpdateSystemJobRecords(IOrganizationService service, List entities)
            {
                // Create an ExecuteMultipleRequest object.
                var multipleRequest = 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()
                };
    
                // Add a UpdateRequest for each entity to the request collection.
                foreach (var entity in entities)
                {
                    UpdateRequest updateRequest = new UpdateRequest { Target = entity };
                    multipleRequest.Requests.Add(updateRequest);
                }
    
                // Execute all the requests in the request collection using a single web method call.
                try
                {
                    ExecuteMultipleResponse multipleResponse = (ExecuteMultipleResponse)service.Execute(multipleRequest);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error");
                    Console.ReadKey();
                }
    
            }
            
    
            public static string CreateXml(string xml, string cookie, int page, int count)
            {
                StringReader stringReader = new StringReader(xml);
                XmlTextReader reader = new XmlTextReader(stringReader);
    
                // Load document
                XmlDocument doc = new XmlDocument();
                doc.Load(reader);
    
                return CreateXml(doc, cookie, page, count);
            }
    
            public static string CreateXml(XmlDocument doc, string cookie, int page, int count)
            {
                XmlAttributeCollection attrs = doc.DocumentElement.Attributes;
    
                if (cookie != null)
                {
                    XmlAttribute pagingAttr = doc.CreateAttribute("paging-cookie");
                    pagingAttr.Value = cookie;
                    attrs.Append(pagingAttr);
                }
    
                XmlAttribute pageAttr = doc.CreateAttribute("page");
                pageAttr.Value = System.Convert.ToString(page);
                attrs.Append(pageAttr);
    
                XmlAttribute countAttr = doc.CreateAttribute("count");
                countAttr.Value = System.Convert.ToString(count);
                attrs.Append(countAttr);
    
                StringBuilder sb = new StringBuilder(1024);
                StringWriter stringWriter = new StringWriter(sb);
    
                XmlTextWriter writer = new XmlTextWriter(stringWriter);
                doc.WriteTo(writer);
                writer.Close();
    
                return sb.ToString();
            }
        }
    }
    

    For log file you can append the log code below link

    https://docs.microsoft.com/en-us/dotnet/standard/io/how-to-open-and-append-to-a-log-file

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans