locked
Using Parallel.ForEach for insert and update CRM data RRS feed

  • Question

  • Hi. I need to update CRM data from external table. All working fine but its very slow. This is my code:

    static void Main(string[] args)
    {
    var dbClient = new CollectionEntities(); //Get database Entities
    using(var xrm = new XrmServiceContext("Xrm"); // Get CRM Entities
        {
    foreach (var row in dbClient.Client) //Reading rows from database
    {
    var c = (from a in crm.new_clientSet where a.new_Idnumber == row.Client_ID select a).FirstOrDefault(); // IS there clint with id from database
                                if (c == null)// if client not exist i create new if exists I update data
                                {
                                    c = new new_client { new_Idnumber = row.Client_ID };
                                    crm.AddObject(c);
                                }
                                c.new_name = row.Client_name; //[Client_name]
                                c.new_Idnumber = row.Client_ID;//[Client_ID]
                                c.EmailAddress = row.Email;//[Email]
       			    xrm.AddObject(c);
        			    xrm.SaveChanges();
    
    }
    }
    }
    With this I can insert and update data in CRM but its to slow. Is there a way to use for this Parallel.ForEach method or some other method to speed up this?  Thanks!

    Sunday, June 5, 2016 11:09 AM

All replies

  • After looking at your code, it seems that you are updating CRM records(i.e. sending an update request to CRM) one by one inside results of the external dataset. This is going to be slow if you are working with a large dataset. 
     
    You could improve performance by using “ExecuteMultipleRequest()”.When you are working with a large number of organization messages you should use ExecuteMultipleRequest.
     
    Following is the link which has a sample code for ExecuteMultipleRequest.

     

    Thanks,

    Sam


    Inogic | Blog | news@inogic.com

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Monday, June 6, 2016 12:45 PM
  • Hi,

    Your below code is added in the For each loop.

    xrm.AddObject(c);

    xrm.SaveChanges();

    you can add the xrm.SaveChanges() outside it. It will help to increase the performance.

    Monday, June 6, 2016 1:13 PM